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"
9#include "ColorMapping.h"
13 template<typename TDataType>
14 VectorVisualNode<TDataType>::VectorVisualNode()
17 this->varLength()->setRange(0, 02);
18 this->varLineWidth()->setRange(0,1);
19 this->stateNormalSet()->setDataPtr(std::make_shared<EdgeSet<DataType3f>>());
21 auto callback = std::make_shared<FCallBackFunc>(std::bind(&VectorVisualNode<TDataType>::varChanged, this));
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);
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);
33 gledge = std::make_shared<GLWireframeVisualModule>();
34 this->stateNormalSet()->connect(gledge->inEdgeSet());
35 this->graphicsPipeline()->pushModule(gledge);
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);
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);
51 this->stateArrowCylinder()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
54 glInstanceCylinder = std::make_shared<GLInstanceVisualModule>();
55 glInstanceCylinder->setColor(Color(0, 1, 0));
57 this->stateArrowCylinder()->connect(glInstanceCylinder->inTriangleSet());
58 this->stateTransformsCylinder()->connect(glInstanceCylinder->inInstanceTransform());
59 colorMapper->outColor()->connect(glInstanceCylinder->inInstanceColor());
60 this->graphicsPipeline()->pushModule(glInstanceCylinder);
63 this->stateArrowCone()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
66 glInstanceCone = std::make_shared<GLInstanceVisualModule>();
67 glInstanceCone->setColor(Color(0, 1, 0));
69 this->stateArrowCone()->connect(glInstanceCone->inTriangleSet());
70 this->stateTransformsCone()->connect(glInstanceCone->inInstanceTransform());
71 colorMapper->outColor()->connect(glInstanceCone->inInstanceColor());
72 this->graphicsPipeline()->pushModule(glInstanceCone);
76 template<typename TDataType>
77 void VectorVisualNode<TDataType>::renderChanged()
79 gledge->varRenderMode()->setCurrentKey(this->varLineMode()->getDataPtr()->currentKey());
80 gledge->varRadius()->setValue(this->varLineWidth()->getValue());
82 if (this->varLineMode()->getValue() == this->Arrow)
84 glInstanceCone->setVisible(true);
85 glInstanceCylinder->setVisible(true);
89 glInstanceCone->setVisible(false);
90 glInstanceCylinder->setVisible(false);
96 template<typename TDataType>
97 void VectorVisualNode<TDataType>::resetStates()
100 this->renderChanged();
103 template<typename TDataType>
104 void VectorVisualNode<TDataType>::updateStates()
107 //this->renderChanged();
110 template<typename TDataType>
111 void VectorVisualNode<TDataType>::varChanged()
113 printf("varChanged\n");
114 if (this->inPointSet()->isEmpty())
116 printf("VectorVisualNode: Need input!\n");
119 auto normalSet = this->stateNormalSet()->getDataPtr();
121 auto ptSet = this->inPointSet()->getDataPtr();
122 if (this->inInVector()->isEmpty() | this->inPointSet()->isEmpty())
124 printf("Normal Node: Need input!\n");
128 d_points = ptSet->getPoints();
129 d_edges.resize(d_points.size());
130 d_normalPt.resize(d_points.size() * 2);
132 cuExecute(d_points.size(),
136 this->inInVector()->getData(),
138 this->varLength()->getValue(),
139 this->varNormalize()->getValue()
142 normalSet->setPoints(d_normalPt);
143 normalSet->setEdges(d_edges);
147 if (this->varLineMode()->getValue() != this->Arrow)
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();
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();
169 auto merge = std::make_shared<TriangleSet<DataType3f>>();
170 merge->copyFrom(*coneTriSet->merge(*cylinderTriSet));
172 this->stateArrowCylinder()->setDataPtr(cylinder->stateTriangleSet()->getDataPtr());
174 DArray<Transform3f> transformCylinder;
175 transformCylinder.resize(d_normalPt.size() / 2);
176 cuExecute(d_normalPt.size()/2,
181 this->varDebug()->getValue(),
186 this->stateTransformsCylinder()->assign(transformCylinder);
187 this->stateArrowCone()->setDataPtr(cone->stateTriangleSet()->getDataPtr());
189 DArray<Transform3f> transformCone;
190 transformCone.resize(d_normalPt.size() / 2);
191 cuExecute(d_normalPt.size() / 2,
196 this->varDebug()->getValue(),
201 this->stateTransformsCone()->assign(transformCone);
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,
216 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
217 if (pId >= d_point.size()) return;
218 Coord dirNormal = normal[pId];
221 dirNormal = dirNormal.normalize() * length;
223 dirNormal = dirNormal * length;
225 normal_points[2 * pId] = d_point[pId] ;
226 normal_points[2 * pId + 1] = d_point[pId] + dirNormal;
228 edges[pId] = TopologyModule::Edge(2 * pId, 2 * pId + 1);
232 template< typename Coord>
233 __global__ void UpdateArrowPoint(
234 DArray<Coord> NormalPt,
235 DArray<Transform3f> transform,
242 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
243 if (pId >= NormalPt.size()/2) return;
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;
252 float length = direction.norm();
253 if (lengthOverride != 0)
254 length = lengthOverride;
256 Vec3f distance = Vec3f(0,direction.norm(),0);
258 direction.normalize();
259 defaultDirection.normalize();
261 Vec3f Axis = direction.cross(defaultDirection);
262 if (Axis == Vec3f(0, 0, 0) && direction[1] < 0)
264 Axis = Vec3f(1, 0, 0);
269 angle = -1 * acos(direction.dot(defaultDirection));
271 Quat<Real> quat = Quat<Real>(angle, Axis);
277 location = root + offest + quat.rotate(distance);
278 scale = Vec3f(1, 1, 1);
282 location = root + offest;
283 scale = Vec3f(1, length, 1);
286 transform[pId].translation() = location;
287 transform[pId].scale() = scale;
288 transform[pId].rotation() = quat.toMatrix3x3();
292 DEFINE_CLASS(VectorVisualNode);