PeriDyno 1.0.0
Loading...
Searching...
No Matches
PointClip.cu
Go to the documentation of this file.
1#include "PointClip.h"
2
3#include "GLSurfaceVisualModule.h"
4#include "GLWireframeVisualModule.h"
5
6
7
8namespace dyno
9{
10 template<typename TDataType>
11 PointClip<TDataType>::PointClip()
12 {
13 this->stateClipPlane()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
14 this->statePointSet()->setDataPtr(std::make_shared<PointSet<TDataType>>());
15 auto plane = this->stateClipPlane()->getDataPtr();
16 auto size = this->varPlaneSize()->getData();
17
18 std::vector<TopologyModule::Triangle> triangles;
19
20 planeVertices.push_back(Coord(0, size, size));
21 planeVertices.push_back(Coord(0, size, -size));
22 planeVertices.push_back(Coord(0, -size, -size));
23 planeVertices.push_back(Coord(0, -size, size));
24
25 triangles.push_back(TopologyModule::Triangle(0,1,2));
26 triangles.push_back(TopologyModule::Triangle(2,3,0));
27
28 plane->setPoints(planeVertices);
29 plane->setTriangles(triangles);
30
31 surface = std::make_shared<GLSurfaceVisualModule>();
32 glpoint = std::make_shared<GLPointVisualModule>();
33
34 surface->setAlpha(0.4);
35 surface->setColor(Color(0,0,0));
36 this->stateClipPlane()->connect(surface->inTriangleSet());
37;
38 glpoint->setColor(this->varPointColor()->getData());
39 glpoint->varPointSize()->setValue(this->varPointSize()->getData());
40 this->statePointSet()->connect(glpoint->inPointSet());
41
42 this->graphicsPipeline()->pushModule(surface);
43 this->graphicsPipeline()->pushModule(glpoint);
44 this->statePointSet()->promoteOuput();
45
46
47 }
48
49 template<typename TDataType>
50 void PointClip<TDataType>::resetStates()
51 {
52 transformPlane();
53 clip();
54 showPlane();
55 glpoint->setColor(this->varPointColor()->getData());
56
57 }
58
59 template<typename TDataType>
60 void PointClip<TDataType>::updateStates()
61 {
62 transformPlane();
63 clip();
64 showPlane();
65 }
66
67 template<typename TDataType>
68 void PointClip<TDataType>::clip()
69 {
70 auto plane = this->stateClipPlane()->getDataPtr();
71
72 auto pointSet = this->inPointSet()->getDataPtr();
73 auto pointSetState = this->statePointSet()->getDataPtr();
74
75 CArray<Coord> planePoint;
76 planePoint.assign(plane->getPoints());
77 Vec3f planeV1 = planePoint[0] - planePoint[1];
78 Vec3f planeV2 = planePoint[1] - planePoint[2];
79 planeV1.normalize();
80 planeV2.normalize();
81 Normal = planeV1.cross(planeV2);
82 Vec3f planeV3;
83
84
85 CArray<Coord> c_point;
86 std::vector<Coord> vertices;
87 c_point.assign(pointSet->getPoints());
88
89 for (size_t i = 0; i < c_point.size(); i++)
90 {
91 if (this->varReverse()->getData()) { planeV3 = c_point[i]-planePoint[0];}
92 else { planeV3 = planePoint[0] - c_point[i]; }
93 if (planeV3.dot(Normal) >= 0)
94 {
95 vertices.push_back(c_point[i]);
96 }
97 }
98 pointSetState->getPoints().clear();
99 pointSetState->setPoints(vertices);
100 pointSetState->update();
101 vertices.clear();
102 planePoint.clear();
103 c_point.clear();
104 }
105
106
107 template<typename TDataType>
108 void PointClip<TDataType>::transformPlane()
109 {
110 auto plane = this->stateClipPlane()->getDataPtr();
111
112 auto scale = this->varScale()->getData();
113 auto rot = this->varRotation()->getData();
114 auto center = this->varLocation()->getData();
115
116 std::vector<Coord> vertices;
117
118 for (size_t i = 0; i < planeVertices.size(); i++)
119 {
120 vertices.push_back(planeVertices[i]);
121 }
122
123
124 Quat<Real> q = this->computeQuaternion();
125
126 q.normalize();
127
128 auto RV = [&](const Coord& v)->Coord {
129 return center + q.rotate(v - center);
130 };
131
132 int numpt = vertices.size();
133
134 for (int i = 0; i < numpt; i++)
135 {
136 vertices[i] = RV(vertices[i] * scale + RV(center));
137 }
138 plane->getPoints().clear();
139 plane->setPoints(vertices);
140 vertices.clear();
141
142 }
143
144 template<typename TDataType>
145 void PointClip<TDataType>::showPlane()
146 {
147 surface->setVisible(this->varShowPlane()->getData());
148 }
149 DEFINE_CLASS(PointClip);
150}