Blender V4.3
unaligned.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "bvh/unaligned.h"
6
7#include "scene/hair.h"
8#include "scene/object.h"
9
10#include "bvh/binning.h"
11#include "bvh/params.h"
12
13#include "util/boundbox.h"
14#include "util/transform.h"
15
17
18BVHUnaligned::BVHUnaligned(const vector<Object *> &objects) : objects_(objects) {}
19
21 const BVHReference *references) const
22{
23 for (int i = range.start(); i < range.end(); ++i) {
24 const BVHReference &ref = references[i];
25 Transform aligned_space;
26 /* Use first primitive which defines correct direction to define
27 * the orientation space.
28 */
29 if (compute_aligned_space(ref, &aligned_space)) {
30 return aligned_space;
31 }
32 }
33 return transform_identity();
34}
35
37 const BVHReference *references) const
38{
39 for (int i = range.start(); i < range.end(); ++i) {
40 const BVHReference &ref = references[i];
41 Transform aligned_space;
42 /* Use first primitive which defines correct direction to define
43 * the orientation space.
44 */
45 if (compute_aligned_space(ref, &aligned_space)) {
46 return aligned_space;
47 }
48 }
49 return transform_identity();
50}
51
52bool BVHUnaligned::compute_aligned_space(const BVHReference &ref, Transform *aligned_space) const
53{
54 const Object *object = objects_[ref.prim_object()];
55 const int packed_type = ref.prim_type();
56 const int type = (packed_type & PRIMITIVE_ALL);
57 /* No motion blur curves here, we can't fit them to aligned boxes well. */
58 if ((type & PRIMITIVE_CURVE) && !(type & PRIMITIVE_MOTION)) {
59 const int curve_index = ref.prim_index();
60 const int segment = PRIMITIVE_UNPACK_SEGMENT(packed_type);
61 const Hair *hair = static_cast<const Hair *>(object->get_geometry());
62 const Hair::Curve &curve = hair->get_curve(curve_index);
63 const int key = curve.first_key + segment;
64 const float3 v1 = hair->get_curve_keys()[key], v2 = hair->get_curve_keys()[key + 1];
65 float length;
66 const float3 axis = normalize_len(v2 - v1, &length);
67 if (length > 1e-6f) {
68 *aligned_space = make_transform_frame(axis);
69 return true;
70 }
71 }
72 *aligned_space = transform_identity();
73 return false;
74}
75
77 const Transform &aligned_space) const
78{
80 const Object *object = objects_[prim.prim_object()];
81 const int packed_type = prim.prim_type();
82 const int type = (packed_type & PRIMITIVE_ALL);
83 /* No motion blur curves here, we can't fit them to aligned boxes well. */
84 if ((type & PRIMITIVE_CURVE) && !(type & PRIMITIVE_MOTION)) {
85 const int curve_index = prim.prim_index();
86 const int segment = PRIMITIVE_UNPACK_SEGMENT(packed_type);
87 const Hair *hair = static_cast<const Hair *>(object->get_geometry());
88 const Hair::Curve &curve = hair->get_curve(curve_index);
89 curve.bounds_grow(
90 segment, &hair->get_curve_keys()[0], &hair->get_curve_radius()[0], aligned_space, bounds);
91 }
92 else {
93 bounds = prim.bounds().transformed(&aligned_space);
94 }
95 return bounds;
96}
97
99 const BVHReference *references,
100 const Transform &aligned_space,
101 BoundBox *cent_bounds) const
102{
104 if (cent_bounds != NULL) {
105 *cent_bounds = BoundBox::empty;
106 }
107 for (int i = range.start(); i < range.end(); ++i) {
108 const BVHReference &ref = references[i];
109 BoundBox ref_bounds = compute_aligned_prim_boundbox(ref, aligned_space);
110 bounds.grow(ref_bounds);
111 if (cent_bounds != NULL) {
112 cent_bounds->grow(ref_bounds.center2());
113 }
114 }
115 return bounds;
116}
117
119 const BVHReference *references,
120 const Transform &aligned_space,
121 BoundBox *cent_bounds) const
122{
124 if (cent_bounds != NULL) {
125 *cent_bounds = BoundBox::empty;
126 }
127 for (int i = range.start(); i < range.end(); ++i) {
128 const BVHReference &ref = references[i];
129 BoundBox ref_bounds = compute_aligned_prim_boundbox(ref, aligned_space);
130 bounds.grow(ref_bounds);
131 if (cent_bounds != NULL) {
132 cent_bounds->grow(ref_bounds.center2());
133 }
134 }
135 return bounds;
136}
137
139 const Transform &aligned_space)
140{
141 Transform space = aligned_space;
142 space.x.w -= bounds.min.x;
143 space.y.w -= bounds.min.y;
144 space.z.w -= bounds.min.z;
145 float3 dim = bounds.max - bounds.min;
146 return transform_scale(
147 1.0f / max(1e-18f, dim.x), 1.0f / max(1e-18f, dim.y), 1.0f / max(1e-18f, dim.z)) *
148 space;
149}
150
ATTR_WARN_UNUSED_RESULT const BMVert * v2
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
__forceinline int prim_type() const
Definition params.h:217
__forceinline int prim_object() const
Definition params.h:213
__forceinline const BoundBox & bounds() const
Definition params.h:205
__forceinline int prim_index() const
Definition params.h:209
BVHUnaligned(const vector< Object * > &objects)
Definition unaligned.cpp:18
Transform compute_aligned_space(const BVHObjectBinning &range, const BVHReference *references) const
Definition unaligned.cpp:20
BoundBox compute_aligned_boundbox(const BVHObjectBinning &range, const BVHReference *references, const Transform &aligned_space, BoundBox *cent_bounds=NULL) const
Definition unaligned.cpp:98
const vector< Object * > & objects_
Definition unaligned.h:56
BoundBox compute_aligned_prim_boundbox(const BVHReference &prim, const Transform &aligned_space) const
Definition unaligned.cpp:76
static Transform compute_node_transform(const BoundBox &bounds, const Transform &aligned_space)
Definition hair.h:14
#define CCL_NAMESPACE_END
#define NULL
@ PRIMITIVE_ALL
@ PRIMITIVE_MOTION
@ PRIMITIVE_CURVE
#define PRIMITIVE_UNPACK_SEGMENT(type)
ccl_device_inline float2 normalize_len(const float2 a, ccl_private float *t)
BoundBox transformed(const Transform *tfm) const
Definition boundbox.h:135
__forceinline void grow(const float3 &pt)
Definition boundbox.h:36
__forceinline float3 center2() const
Definition boundbox.h:118
void bounds_grow(const int k, const float3 *curve_keys, const float *curve_radius, BoundBox &bounds) const
Definition hair.cpp:42
int first_key
Definition hair.h:20
float4 x
Definition transform.h:24
float z
Definition sky_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
ccl_device_inline Transform make_transform_frame(float3 N)
Definition transform.h:202
ccl_device_inline Transform transform_identity()
Definition transform.h:296
ccl_device_inline Transform transform_scale(float3 s)
Definition transform.h:254
float max