PeriDyno 1.0.0
Loading...
Searching...
No Matches
VectorVisualNode.cu
Go to the documentation of this file.
1#include "VectorVisualNode.h"
2#include "Topology/PointSet.h"
3#include "GLSurfaceVisualModule.h"
4#include "GLWireframeVisualModule.h"
5#include "GLPointVisualModule.h"
6#include "BasicShapes/CylinderModel.h"
7#include "BasicShapes/ConeModel.h"
8
9#include "ColorMapping.h"
10
11namespace dyno
12{
13 template<typename TDataType>
14 VectorVisualNode<TDataType>::VectorVisualNode()
15 : Node()
16 {
17 this->varLength()->setRange(0, 02);
18 this->varLineWidth()->setRange(0,1);
19 this->stateNormalSet()->setDataPtr(std::make_shared<EdgeSet<DataType3f>>());
20
21 auto callback = std::make_shared<FCallBackFunc>(std::bind(&VectorVisualNode<TDataType>::varChanged, this));
22
23 this->varLength()->attach(callback);
24 this->varDebug()->attach(callback);
25 this->varLineWidth()->attach(callback);
26 this->varArrowResolution()->attach(callback);
27 this->varLineMode()->attach(callback);
28
29 auto render_callback = std::make_shared<FCallBackFunc>(std::bind(&VectorVisualNode<TDataType>::renderChanged, this));
30 this->varLineMode()->attach(render_callback);
31 this->varLineWidth()->attach(render_callback);
32
33 gledge = std::make_shared<GLWireframeVisualModule>();
34 this->stateNormalSet()->connect(gledge->inEdgeSet());
35 this->graphicsPipeline()->pushModule(gledge);
36
37 this->varArrowResolution()->setRange(4,15);
38 this->varLength()->setRange(0.1,10);
39 this->inPointSet()->tagOptional(true);
40 this->inInVector()->tagOptional(true);
41 this->inScalar()->tagOptional(true);
42
43
44 auto colorMapper = std::make_shared<ColorMapping<DataType3f>>();
45 colorMapper->varMin()->setValue(-0.5);
46 colorMapper->varMax()->setValue(0.5);
47 this->inScalar()->connect(colorMapper->inScalar());
48 this->graphicsPipeline()->pushModule(colorMapper);
49
50 //cylinder
51 this->stateArrowCylinder()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
52
53
54 glInstanceCylinder = std::make_shared<GLInstanceVisualModule>();
55 glInstanceCylinder->setColor(Color(0, 1, 0));
56
57 this->stateArrowCylinder()->connect(glInstanceCylinder->inTriangleSet());
58 this->stateTransformsCylinder()->connect(glInstanceCylinder->inInstanceTransform());
59 colorMapper->outColor()->connect(glInstanceCylinder->inInstanceColor());
60 this->graphicsPipeline()->pushModule(glInstanceCylinder);
61
62 //cone
63 this->stateArrowCone()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
64
65
66 glInstanceCone = std::make_shared<GLInstanceVisualModule>();
67 glInstanceCone->setColor(Color(0, 1, 0));
68
69 this->stateArrowCone()->connect(glInstanceCone->inTriangleSet());
70 this->stateTransformsCone()->connect(glInstanceCone->inInstanceTransform());
71 colorMapper->outColor()->connect(glInstanceCone->inInstanceColor());
72 this->graphicsPipeline()->pushModule(glInstanceCone);
73
74 }
75
76 template<typename TDataType>
77 void VectorVisualNode<TDataType>::renderChanged()
78 {
79 gledge->varRenderMode()->setCurrentKey(this->varLineMode()->getDataPtr()->currentKey());
80 gledge->varRadius()->setValue(this->varLineWidth()->getValue());
81
82 if (this->varLineMode()->getValue() == this->Arrow)
83 {
84 glInstanceCone->setVisible(true);
85 glInstanceCylinder->setVisible(true);
86 }
87 else
88 {
89 glInstanceCone->setVisible(false);
90 glInstanceCylinder->setVisible(false);
91 }
92
93 }
94
95
96 template<typename TDataType>
97 void VectorVisualNode<TDataType>::resetStates()
98 {
99 this->varChanged();
100 this->renderChanged();
101 }
102
103 template<typename TDataType>
104 void VectorVisualNode<TDataType>::updateStates()
105 {
106 this->varChanged();
107 //this->renderChanged();
108 }
109
110 template<typename TDataType>
111 void VectorVisualNode<TDataType>::varChanged()
112 {
113 printf("varChanged\n");
114 if (this->inPointSet()->isEmpty())
115 {
116 printf("VectorVisualNode: Need input!\n");
117 return;
118 }
119 auto normalSet = this->stateNormalSet()->getDataPtr();
120
121 auto ptSet = this->inPointSet()->getDataPtr();
122 if (this->inInVector()->isEmpty() | this->inPointSet()->isEmpty())
123 {
124 printf("Normal Node: Need input!\n");
125 return;
126 }
127
128 d_points = ptSet->getPoints();
129 d_edges.resize(d_points.size());
130 d_normalPt.resize(d_points.size() * 2);
131
132 cuExecute(d_points.size(),
133 UpdatePointNormal,
134 ptSet->getPoints(),
135 d_normalPt,
136 this->inInVector()->getData(),
137 d_edges,
138 this->varLength()->getValue(),
139 this->varNormalize()->getValue()
140 );
141
142 normalSet->setPoints(d_normalPt);
143 normalSet->setEdges(d_edges);
144 normalSet->update();
145
146
147 if (this->varLineMode()->getValue() != this->Arrow)
148 return;
149
150 // BuildArrow
151 {
152 auto cylinder = std::make_shared<CylinderModel<DataType3f>>();
153 cylinder->varColumns()->setValue(this->varArrowResolution()->getValue());
154 cylinder->varEndSegment()->setValue(1);
155 cylinder->varRow()->setValue(1);
156 cylinder->varRadius()->setValue(this->varLineWidth()->getValue());
157 cylinder->varHeight()->setValue(1.0f);
158 cylinder->varLocation()->setValue(Coord(0.0f, 0.5f, 0.0f));
159 auto cylinderTriSet = cylinder->stateTriangleSet()->getDataPtr();
160
161 auto cone = std::make_shared<ConeModel<DataType3f>>();
162 cone->varColumns()->setValue(this->varArrowResolution()->getValue());
163 cone->varRadius()->setValue(this->varLineWidth()->getValue() * 2);
164 cone->varHeight()->setValue(this->varLineWidth()->getValue() * 2 * 2);
165 cone->varLocation()->setValue(Vec3f(0, this->varLineWidth()->getValue(), 0));
166 cone->varRow()->setValue(1);
167 auto coneTriSet = cone->stateTriangleSet()->getDataPtr();
168
169 auto merge = std::make_shared<TriangleSet<DataType3f>>();
170 merge->copyFrom(*coneTriSet->merge(*cylinderTriSet));
171
172 this->stateArrowCylinder()->setDataPtr(cylinder->stateTriangleSet()->getDataPtr());
173
174 DArray<Transform3f> transformCylinder;
175 transformCylinder.resize(d_normalPt.size() / 2);
176 cuExecute(d_normalPt.size()/2,
177 UpdateArrowPoint,
178 d_normalPt,
179 transformCylinder,
180 0,
181 this->varDebug()->getValue(),
182 false,
183 0
184 );
185
186 this->stateTransformsCylinder()->assign(transformCylinder);
187 this->stateArrowCone()->setDataPtr(cone->stateTriangleSet()->getDataPtr());
188
189 DArray<Transform3f> transformCone;
190 transformCone.resize(d_normalPt.size() / 2);
191 cuExecute(d_normalPt.size() / 2,
192 UpdateArrowPoint,
193 d_normalPt,
194 transformCone,
195 0,
196 this->varDebug()->getValue(),
197 true,
198 0
199 );
200
201 this->stateTransformsCone()->assign(transformCone);
202 }
203 }
204
205
206 template< typename Coord>
207 __global__ void UpdatePointNormal(
208 DArray<Coord> d_point,
209 DArray<Coord> normal_points,
210 DArray<Coord> normal,
211 DArray<TopologyModule::Edge> edges,
212 float length,
213 bool normallization
214 )
215 {
216 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
217 if (pId >= d_point.size()) return;
218 Coord dirNormal = normal[pId];
219
220 if(normallization)
221 dirNormal = dirNormal.normalize() * length;
222 else
223 dirNormal = dirNormal * length;
224
225 normal_points[2 * pId] = d_point[pId] ;
226 normal_points[2 * pId + 1] = d_point[pId] + dirNormal;
227
228 edges[pId] = TopologyModule::Edge(2 * pId, 2 * pId + 1);
229 }
230
231
232 template< typename Coord>
233 __global__ void UpdateArrowPoint(
234 DArray<Coord> NormalPt,
235 DArray<Transform3f> transform,
236 int offest,
237 int debug,
238 bool moveToTop,
239 float lengthOverride
240 )
241 {
242 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
243 if (pId >= NormalPt.size()/2) return;
244
245 int i = pId;
246
247 Coord root = NormalPt[i * 2];
248 Coord head = NormalPt[i * 2 + 1];
249 Vec3f defaultDirection = Vec3f(0, 1, 0);
250 Vec3f direction = head - root;
251
252 float length = direction.norm();
253 if (lengthOverride != 0)
254 length = lengthOverride;
255
256 Vec3f distance = Vec3f(0,direction.norm(),0);
257
258 direction.normalize();
259 defaultDirection.normalize();
260 Real angle;
261 Vec3f Axis = direction.cross(defaultDirection);
262 if (Axis == Vec3f(0, 0, 0) && direction[1] < 0)
263 {
264 Axis = Vec3f(1, 0, 0);
265 angle = M_PI;
266 }
267 Axis.normalize();
268
269 angle = -1 * acos(direction.dot(defaultDirection));
270
271 Quat<Real> quat = Quat<Real>(angle, Axis);
272 Vec3f location;
273 Vec3f scale;
274
275 if (moveToTop)
276 {
277 location = root + offest + quat.rotate(distance);
278 scale = Vec3f(1, 1, 1);
279 }
280 else
281 {
282 location = root + offest;
283 scale = Vec3f(1, length, 1);
284 }
285
286 transform[pId].translation() = location;
287 transform[pId].scale() = scale;
288 transform[pId].rotation() = quat.toMatrix3x3();
289 }
290
291
292 DEFINE_CLASS(VectorVisualNode);
293}