1 /*
2  * Copyright 2011-2020 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __HAIR_H__
18 #define __HAIR_H__
19 
20 #include "render/geometry.h"
21 
22 CCL_NAMESPACE_BEGIN
23 
24 class Hair : public Geometry {
25  public:
26   NODE_DECLARE
27 
28   /* Hair Curve */
29   struct Curve {
30     int first_key;
31     int num_keys;
32 
num_segmentsCurve33     int num_segments() const
34     {
35       return num_keys - 1;
36     }
37 
38     void bounds_grow(const int k,
39                      const float3 *curve_keys,
40                      const float *curve_radius,
41                      BoundBox &bounds) const;
42     void bounds_grow(float4 keys[4], BoundBox &bounds) const;
43     void bounds_grow(const int k,
44                      const float3 *curve_keys,
45                      const float *curve_radius,
46                      const Transform &aligned_space,
47                      BoundBox &bounds) const;
48 
49     void motion_keys(const float3 *curve_keys,
50                      const float *curve_radius,
51                      const float3 *key_steps,
52                      size_t num_curve_keys,
53                      size_t num_steps,
54                      float time,
55                      size_t k0,
56                      size_t k1,
57                      float4 r_keys[2]) const;
58     void cardinal_motion_keys(const float3 *curve_keys,
59                               const float *curve_radius,
60                               const float3 *key_steps,
61                               size_t num_curve_keys,
62                               size_t num_steps,
63                               float time,
64                               size_t k0,
65                               size_t k1,
66                               size_t k2,
67                               size_t k3,
68                               float4 r_keys[4]) const;
69 
70     void keys_for_step(const float3 *curve_keys,
71                        const float *curve_radius,
72                        const float3 *key_steps,
73                        size_t num_curve_keys,
74                        size_t num_steps,
75                        size_t step,
76                        size_t k0,
77                        size_t k1,
78                        float4 r_keys[2]) const;
79     void cardinal_keys_for_step(const float3 *curve_keys,
80                                 const float *curve_radius,
81                                 const float3 *key_steps,
82                                 size_t num_curve_keys,
83                                 size_t num_steps,
84                                 size_t step,
85                                 size_t k0,
86                                 size_t k1,
87                                 size_t k2,
88                                 size_t k3,
89                                 float4 r_keys[4]) const;
90   };
91 
92   array<float3> curve_keys;
93   array<float> curve_radius;
94   array<int> curve_first_key;
95   array<int> curve_shader;
96 
97   /* BVH */
98   size_t curvekey_offset;
99   CurveShapeType curve_shape;
100 
101   /* Constructor/Destructor */
102   Hair();
103   ~Hair();
104 
105   /* Geometry */
106   void clear() override;
107 
108   void resize_curves(int numcurves, int numkeys);
109   void reserve_curves(int numcurves, int numkeys);
110   void add_curve_key(float3 loc, float radius);
111   void add_curve(int first_key, int shader);
112 
113   void copy_center_to_motion_step(const int motion_step);
114 
115   void compute_bounds() override;
116   void apply_transform(const Transform &tfm, const bool apply_to_motion) override;
117 
118   /* Curves */
get_curve(size_t i)119   Curve get_curve(size_t i) const
120   {
121     int first = curve_first_key[i];
122     int next_first = (i + 1 < curve_first_key.size()) ? curve_first_key[i + 1] : curve_keys.size();
123 
124     Curve curve = {first, next_first - first};
125     return curve;
126   }
127 
num_keys()128   size_t num_keys() const
129   {
130     return curve_keys.size();
131   }
132 
num_curves()133   size_t num_curves() const
134   {
135     return curve_first_key.size();
136   }
137 
num_segments()138   size_t num_segments() const
139   {
140     return curve_keys.size() - curve_first_key.size();
141   }
142 
143   /* UDIM */
144   void get_uv_tiles(ustring map, unordered_set<int> &tiles) override;
145 
146   /* BVH */
147   void pack_curves(Scene *scene, float4 *curve_key_co, float4 *curve_data, size_t curvekey_offset);
148 };
149 
150 CCL_NAMESPACE_END
151 
152 #endif /* __HAIR_H__ */
153