Blender V5.0
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
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];
65 const float3 v2 = hair->get_curve_keys()[key + 1];
66 float length;
67 const float3 axis = normalize_len(v2 - v1, &length);
68 if (length > 1e-6f) {
69 *aligned_space = make_transform_frame(axis);
70 return true;
71 }
72 }
73 *aligned_space = transform_identity();
74 return false;
75}
76
78 const Transform &aligned_space) const
79{
81 const Object *object = objects_[prim.prim_object()];
82 const int packed_type = prim.prim_type();
83 const int type = (packed_type & PRIMITIVE_ALL);
84 /* No motion blur curves here, we can't fit them to aligned boxes well. */
85 if ((type & PRIMITIVE_CURVE) && !(type & PRIMITIVE_MOTION)) {
86 const int curve_index = prim.prim_index();
87 const int segment = PRIMITIVE_UNPACK_SEGMENT(packed_type);
88 const Hair *hair = static_cast<const Hair *>(object->get_geometry());
89 const Hair::Curve &curve = hair->get_curve(curve_index);
90 curve.bounds_grow(segment,
91 hair->get_curve_keys().data(),
92 hair->get_curve_radius().data(),
93 aligned_space,
94 bounds);
95 }
96 else {
97 bounds = prim.bounds().transformed(&aligned_space);
98 }
99 return bounds;
100}
101
103 const BVHReference *references,
104 const Transform &aligned_space,
105 BoundBox *cent_bounds) const
106{
108 if (cent_bounds != nullptr) {
109 *cent_bounds = BoundBox::empty;
110 }
111 for (int i = range.start(); i < range.end(); ++i) {
112 const BVHReference &ref = references[i];
113 const BoundBox ref_bounds = compute_aligned_prim_boundbox(ref, aligned_space);
114 bounds.grow(ref_bounds);
115 if (cent_bounds != nullptr) {
116 cent_bounds->grow(ref_bounds.center2());
117 }
118 }
119 return bounds;
120}
121
123 const BVHReference *references,
124 const Transform &aligned_space,
125 BoundBox *cent_bounds) const
126{
128 if (cent_bounds != nullptr) {
129 *cent_bounds = BoundBox::empty;
130 }
131 for (int i = range.start(); i < range.end(); ++i) {
132 const BVHReference &ref = references[i];
133 const BoundBox ref_bounds = compute_aligned_prim_boundbox(ref, aligned_space);
134 bounds.grow(ref_bounds);
135 if (cent_bounds != nullptr) {
136 cent_bounds->grow(ref_bounds.center2());
137 }
138 }
139 return bounds;
140}
141
143 const Transform &aligned_space)
144{
145 Transform space = aligned_space;
146 space.x.w -= bounds.min.x;
147 space.y.w -= bounds.min.y;
148 space.z.w -= bounds.min.z;
149 const float3 dim = bounds.max - bounds.min;
150 return transform_scale(
151 1.0f / max(1e-18f, dim.x), 1.0f / max(1e-18f, dim.y), 1.0f / max(1e-18f, dim.z)) *
152 space;
153}
154
ATTR_WARN_UNUSED_RESULT const BMVert * v2
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
__forceinline int start() const
Definition params.h:277
__forceinline int end() const
Definition params.h:285
__forceinline int prim_type() const
Definition params.h:216
__forceinline int prim_object() const
Definition params.h:212
__forceinline const BoundBox & bounds() const
Definition params.h:204
__forceinline int prim_index() const
Definition params.h:208
BVHUnaligned(const vector< Object * > &objects)
Definition unaligned.cpp:18
Transform compute_aligned_space(const BVHObjectBinning &range, const BVHReference *references) const
Definition unaligned.cpp:20
const vector< Object * > & objects_
Definition unaligned.h:55
BoundBox compute_aligned_prim_boundbox(const BVHReference &prim, const Transform &aligned_space) const
Definition unaligned.cpp:77
BoundBox compute_aligned_boundbox(const BVHObjectBinning &range, const BVHReference *references, const Transform &aligned_space, BoundBox *cent_bounds=nullptr) const
static Transform compute_node_transform(const BoundBox &bounds, const Transform &aligned_space)
Definition hair.h:13
Curve get_curve(const size_t i) const
Definition hair.h:111
#define PRIMITIVE_UNPACK_SEGMENT(type)
#define CCL_NAMESPACE_END
float length(VecOp< float, D >) RET
@ PRIMITIVE_ALL
@ PRIMITIVE_MOTION
@ PRIMITIVE_CURVE
ccl_device_inline float2 normalize_len(const float2 a, ccl_private float *t)
BoundBox transformed(const Transform *tfm) const
Definition boundbox.h:134
__forceinline void grow(const float3 &pt)
Definition boundbox.h:35
__forceinline float3 center2() const
Definition boundbox.h:117
void bounds_grow(const int k, const float3 *curve_keys, const float *curve_radius, BoundBox &bounds) const
Definition hair.cpp:44
int first_key
Definition hair.h:19
float4 y
Definition transform.h:23
float4 x
Definition transform.h:23
float4 z
Definition transform.h:23
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float w
Definition sky_math.h:225
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
ccl_device_inline Transform transform_identity()
Definition transform.h:322
ccl_device_inline Transform make_transform_frame(const float3 N)
Definition transform.h:228
ccl_device_inline Transform transform_scale(const float3 s)
Definition transform.h:280