Skip to content

Commit b5de8c9

Browse files
Mafo369dlyr
authored andcommitted
[tests] Refactor point/curve component creation
1 parent 0fc2380 commit b5de8c9

3 files changed

Lines changed: 58 additions & 43 deletions

File tree

examples/CurveEditor/CurveEditor.cpp

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,27 @@ CurveEditor::CurveEditor( const VectorArray<Curve2D::Vector>& polyline, MyViewer
3636
Vector3( solutionPts[i + 1].x(), 0, solutionPts[i + 1].y() ),
3737
Vector3( solutionPts[i + 2].x(), 0, solutionPts[i + 2].y() ),
3838
Vector3( solutionPts[i + 3].x(), 0, solutionPts[i + 3].y() ) };
39-
std::string name = "Curve_" + std::to_string( i / 3 );
40-
auto e = factory.createCurveComponent( this, ctrlPts, name );
39+
auto e = factory.createCurveComponent( this, ctrlPts, i / 3 );
4140
m_curveEntities.push_back( e );
4241
allCtrlPts.push_back( ctrlPts );
4342
}
4443

45-
std::string namePt = "CtrlPt_" + std::to_string( 0 );
46-
auto e =
47-
PointFactory::createPointComponent( this, allCtrlPts[0][0], { 0 }, namePt, Color::Blue() );
44+
auto e = PointFactory::createPointComponent( this, allCtrlPts[0][0], { 0 }, 0, Color::Blue() );
4845
m_pointEntities.push_back( e );
4946

5047
int nameIndex = 1;
5148
for ( unsigned int i = 0; i < allCtrlPts.size(); i++ ) {
5249
for ( unsigned int j = 1; j < allCtrlPts[i].size() - 1; j++ ) {
53-
namePt = "CtrlPt_" + std::to_string( nameIndex );
54-
e = PointFactory::createPointComponent( this, allCtrlPts[i][j], { i }, namePt );
50+
e = PointFactory::createPointComponent( this, allCtrlPts[i][j], { i }, nameIndex );
5551
m_pointEntities.push_back( e );
5652
nameIndex++;
5753
}
58-
namePt = "CtrlPt_" + std::to_string( nameIndex );
5954
if ( i == allCtrlPts.size() - 1 )
6055
e = PointFactory::createPointComponent(
61-
this, allCtrlPts[i][3], { i }, namePt, Color::Blue() );
56+
this, allCtrlPts[i][3], { i }, nameIndex, Color::Blue() );
6257
else
6358
e = PointFactory::createPointComponent(
64-
this, allCtrlPts[i][3], { i, i + 1 }, namePt, Color::Blue() );
59+
this, allCtrlPts[i][3], { i, i + 1 }, nameIndex, Color::Blue() );
6560
m_pointEntities.push_back( e );
6661
nameIndex++;
6762
}
@@ -180,18 +175,19 @@ inline float CurveEditor::distanceSquared( const Vector3f& pointA, const Vector3
180175
return dx * dx + dy * dy + dz * dz;
181176
}
182177

183-
// De Casteljau algorithm
184178
void CurveEditor::subdivisionBezier( int vertexIndex,
185179
unsigned int curveIndex,
186180
const Vector3Array& ctrlPts ) {
187-
auto bezier = Geometry::CubicBezier( Vector2( ctrlPts[0].x(), ctrlPts[0].z() ),
181+
182+
auto bezier = Geometry::CubicBezier( Vector2( ctrlPts[0].x(), ctrlPts[0].z() ),
188183
Vector2( ctrlPts[1].x(), ctrlPts[1].z() ),
189184
Vector2( ctrlPts[2].x(), ctrlPts[2].z() ),
190185
Vector2( ctrlPts[3].x(), ctrlPts[3].z() ) );
191-
float u = float( vertexIndex ) / 100.f;
186+
float u = float( vertexIndex ) / 100.f;
187+
Vector2 fu = bezier.f( u );
188+
auto clickedPoint = Vector3( fu.x(), 0, fu.y() );
192189

193-
Vector2 fu = bezier.f( u );
194-
auto clickedPoint = Vector3( fu.x(), 0, fu.y() );
190+
// De Casteljau
195191
Vector3 firstPoint = Math::linearInterpolate( ctrlPts[0], ctrlPts[1], u );
196192
Vector3 sndPoint = Math::linearInterpolate( ctrlPts[1], ctrlPts[2], u );
197193
Vector3 thirdPoint = Math::linearInterpolate( ctrlPts[2], ctrlPts[3], u );
@@ -202,24 +198,24 @@ void CurveEditor::subdivisionBezier( int vertexIndex,
202198
newCtrlPts[1] = firstPoint;
203199
newCtrlPts[2] = fourthPoint;
204200
newCtrlPts[3] = clickedPoint;
205-
auto nameCurve = m_curveEntities[curveIndex]->getName();
201+
202+
auto nameCurve = m_curveEntities[curveIndex]->getName();
206203
removeComponent( nameCurve );
207204
m_curveEntities[curveIndex] = CurveFactory::createCurveComponent( this, newCtrlPts, nameCurve );
208205

209206
refreshPoint( curveIndex * 3 + 1, firstPoint );
210207
refreshPoint( curveIndex * 3 + 2, thirdPoint );
211208

212209
auto firstInsertionIdx = curveIndex * 3 + 2;
213-
std::string namePt = "CtrlPt_" + std::to_string( m_pointEntities.size() );
214210
auto ptE = PointFactory::createPointComponent(
215-
this, clickedPoint, { curveIndex, curveIndex + 1 }, namePt, Color::Blue() );
211+
this, clickedPoint, { curveIndex, curveIndex + 1 }, m_pointEntities.size(), Color::Blue() );
216212
m_pointEntities.insert( m_pointEntities.begin() + firstInsertionIdx, ptE );
217-
namePt = "CtrlPt_" + std::to_string( m_pointEntities.size() );
218-
ptE = PointFactory::createPointComponent( this, fourthPoint, { curveIndex }, namePt );
213+
ptE = PointFactory::createPointComponent(
214+
this, fourthPoint, { curveIndex }, m_pointEntities.size() );
219215
m_pointEntities.insert( m_pointEntities.begin() + firstInsertionIdx, ptE );
220216

221-
namePt = "CtrlPt_" + std::to_string( m_pointEntities.size() );
222-
ptE = PointFactory::createPointComponent( this, fifthPoint, { curveIndex + 1 }, namePt );
217+
ptE = PointFactory::createPointComponent(
218+
this, fifthPoint, { curveIndex + 1 }, m_pointEntities.size() );
223219
m_pointEntities.insert( m_pointEntities.begin() + ( ( curveIndex + 1 ) * 3 + 1 ), ptE );
224220

225221
Vector3Array newCtrlPts1;
@@ -228,8 +224,7 @@ void CurveEditor::subdivisionBezier( int vertexIndex,
228224
newCtrlPts1.push_back( thirdPoint );
229225
newCtrlPts1.push_back( ctrlPts[3] );
230226

231-
nameCurve = "Curve_" + std::to_string( m_curveEntities.size() );
232-
auto curveE = CurveFactory::createCurveComponent( this, newCtrlPts1, nameCurve );
227+
auto curveE = CurveFactory::createCurveComponent( this, newCtrlPts1, m_curveEntities.size() );
233228

234229
m_curveEntities.insert( m_curveEntities.begin() + curveIndex + 1, curveE );
235230

@@ -242,37 +237,32 @@ void CurveEditor::subdivisionBezier( int vertexIndex,
242237
}
243238

244239
void CurveEditor::addPointAtEnd( const Vector3& worldPos ) {
245-
Vector3Array ctrlPts;
246240
auto lastIndex = m_pointEntities.size() - 1;
247241
auto beforeLast = m_pointEntities[lastIndex - 1];
248242
auto last = m_pointEntities[lastIndex];
249243
Vector3 diff = ( last->m_point - beforeLast->m_point );
250244
diff.normalize();
245+
251246
Vector3 secondPt = ( last->m_point + diff );
252247
Vector3 thirdDiff = ( last->m_point - worldPos );
253248
thirdDiff.normalize();
254249
Vector3 thirdPt = ( worldPos - diff );
255-
ctrlPts.push_back( last->m_point );
256-
ctrlPts.push_back( secondPt );
257-
ctrlPts.push_back( thirdPt );
258-
ctrlPts.push_back( worldPos );
259-
std::string name = "Curve_" + std::to_string( m_curveEntities.size() );
260-
auto e = CurveFactory::createCurveComponent( this, ctrlPts, name );
250+
251+
Vector3Array ctrlPts { last->m_point, secondPt, thirdPt, worldPos };
252+
253+
auto e = CurveFactory::createCurveComponent( this, ctrlPts, m_curveEntities.size() );
261254
m_curveEntities.push_back( e );
262255

263256
unsigned int pointIndex = lastIndex + 1;
264257
last->m_curveId.push_back( ( pointIndex / 3 ) );
265-
std::string namePt = "CtrlPt_" + std::to_string( pointIndex );
266258
auto ptC =
267-
PointFactory::createPointComponent( this, ctrlPts[1], { ( pointIndex / 3 ) }, namePt );
259+
PointFactory::createPointComponent( this, ctrlPts[1], { ( pointIndex / 3 ) }, pointIndex );
268260
m_pointEntities.push_back( ptC );
269-
namePt = "CtrlPt_" + std::to_string( pointIndex + 1 );
270-
auto eb =
271-
PointFactory::createPointComponent( this, ctrlPts[2], { ( pointIndex / 3 ) }, namePt );
261+
auto eb = PointFactory::createPointComponent(
262+
this, ctrlPts[2], { ( pointIndex / 3 ) }, pointIndex + 1 );
272263
m_pointEntities.push_back( eb );
273-
namePt = "CtrlPt_" + std::to_string( pointIndex + 2 );
274-
ptC = PointFactory::createPointComponent(
275-
this, ctrlPts[3], { ( pointIndex / 3 ) }, namePt, Color::Blue() );
264+
ptC = PointFactory::createPointComponent(
265+
this, ctrlPts[3], { ( pointIndex / 3 ) }, pointIndex + 2, Color::Blue() );
276266
m_pointEntities.push_back( ptC );
277267
}
278268

@@ -281,16 +271,17 @@ void CurveEditor::addPointInCurve( const Vector3& worldPos, int mouseX, int mous
281271
auto radius = int( m_viewer->getRenderer()->getBrushRadius() );
282272
bool found = false;
283273
Rendering::Renderer::PickingResult pres;
274+
284275
for ( int i = mouseX - radius; i < mouseX + radius && !found; i++ ) {
285276
for ( int j = mouseY - radius; j < mouseY + radius; j++ ) {
286-
// m_viewer->cursor().setPos(m_viewer->mapToGlobal(QPoint(i, j)));
277+
287278
Rendering::Renderer::PickingQuery query { Vector2( i, m_viewer->height() - j ),
288279
Rendering::Renderer::SELECTION,
289280
Rendering::Renderer::RO };
290281
Data::ViewingParameters renderData {
291282
camera->getViewMatrix(), camera->getProjMatrix(), 0 };
292-
293283
pres = m_viewer->getRenderer()->doPickingNow( query, renderData );
284+
294285
if ( pres.getRoIdx().isValid() && m_roMgr->exists( pres.getRoIdx() ) ) {
295286
auto ro = m_roMgr->getRenderObject( pres.getRoIdx() );
296287
if ( ro->getMesh()->getNumVertices() == 2 ) continue;
@@ -324,9 +315,9 @@ void CurveEditor::addPointInCurve( const Vector3& worldPos, int mouseX, int mous
324315
}
325316

326317
subdivisionBezier( vIndex, curveIndex, ctrlPts );
318+
found = true;
319+
break;
327320
}
328-
found = true;
329-
break;
330321
}
331322
}
332323
}
@@ -380,6 +371,7 @@ void CurveEditor::processPicking( const Vector3& worldPos ) {
380371
computePointTransform( pointComponent, pointMid->m_point, worldPos );
381372
auto ro = m_roMgr->getRenderObject( pointComponent->getRenderObjects()[0] );
382373
ro->setLocalTransform( symTransform );
374+
383375
if ( m_tangentPoints.empty() ) { m_tangentPoints.push_back( m_currentPoint + 2 ); }
384376
}
385377
else if ( m_currentPoint % 3 == 1 ) {
@@ -389,17 +381,20 @@ void CurveEditor::processPicking( const Vector3& worldPos ) {
389381
computePointTransform( pointComponent, pointMid->m_point, worldPos );
390382
auto ro = m_roMgr->getRenderObject( pointComponent->getRenderObjects()[0] );
391383
ro->setLocalTransform( symTransform );
384+
392385
if ( m_tangentPoints.empty() ) { m_tangentPoints.push_back( m_currentPoint - 2 ); }
393386
}
394387
else if ( m_currentPoint % 3 == 0 ) {
395388
auto leftRo = m_roMgr->getRenderObject(
396389
m_pointEntities[m_currentPoint - 1]->getRenderObjects()[0] );
397390
auto leftTransform = leftRo->getTransform() * transformTranslate;
398391
leftRo->setLocalTransform( leftTransform );
392+
399393
auto rightRo = m_roMgr->getRenderObject(
400394
m_pointEntities[m_currentPoint + 1]->getRenderObjects()[0] );
401395
auto rightTransform = rightRo->getTransform() * transformTranslate;
402396
rightRo->setLocalTransform( rightTransform );
397+
403398
if ( m_tangentPoints.empty() ) {
404399
m_tangentPoints.push_back( m_currentPoint - 1 );
405400
m_tangentPoints.push_back( m_currentPoint + 1 );

examples/CurveEditor/CurveFactory.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
class CurveFactory
88
{
99
public:
10+
static CurveComponent*
11+
createCurveComponent( Ra::Engine::Scene::Entity* e, Ra::Core::Vector3Array ctrlPts, int id ) {
12+
std::string name = "Curve_" + std::to_string( id );
13+
auto c = new CurveComponent( e, ctrlPts, name );
14+
c->initialize();
15+
return c;
16+
}
1017
static CurveComponent* createCurveComponent( Ra::Engine::Scene::Entity* e,
1118
Ra::Core::Vector3Array ctrlPts,
1219
const std::string& name ) {

examples/CurveEditor/PointFactory.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
class PointFactory
99
{
1010
public:
11+
static PointComponent*
12+
createPointComponent( Ra::Engine::Scene::Entity* e,
13+
Ra::Core::Vector3 point,
14+
const std::vector<unsigned int>& curveId,
15+
int id,
16+
Ra::Core::Utils::Color color = Ra::Core::Utils::Color::Black() ) {
17+
std::string name = "CtrlPt_" + std::to_string( id );
18+
auto c = new PointComponent( e, point, curveId, name, color );
19+
c->initialize();
20+
return c;
21+
}
22+
1123
static PointComponent*
1224
createPointComponent( Ra::Engine::Scene::Entity* e,
1325
Ra::Core::Vector3 point,
@@ -18,6 +30,7 @@ class PointFactory
1830
c->initialize();
1931
return c;
2032
}
33+
2134
static Ra::Engine::Scene::Entity*
2235
createPointEntity( Ra::Core::Vector3 point,
2336
const std::string& name,

0 commit comments

Comments
 (0)