Skip to content

Commit 8c6ff55

Browse files
Mafo369dlyr
authored andcommitted
[tests] Change smoothness and symetry to be a point attribute
1 parent b2a0f61 commit 8c6ff55

5 files changed

Lines changed: 200 additions & 26 deletions

File tree

examples/CurveEditor/CurveEditor.cpp

Lines changed: 133 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void CurveEditor::refreshPoint( unsigned int pointIndex, const Vector3& newPoint
104104
auto pointComponent = m_pointEntities[pointIndex];
105105
auto pointColor = pointComponent->m_color;
106106
auto pointCurve = pointComponent->m_curveId;
107+
auto pointState = pointComponent->m_state;
107108
Vector3 point;
108109
if ( newPoint == Vector3::Zero() )
109110
point = pointComponent->m_point;
@@ -112,6 +113,7 @@ void CurveEditor::refreshPoint( unsigned int pointIndex, const Vector3& newPoint
112113
removeComponent( m_pointEntities[pointIndex]->getName() );
113114
auto pointEntity =
114115
PointFactory::createPointComponent( this, point, pointCurve, pointName, pointColor );
116+
pointEntity->m_state = pointState;
115117
m_pointEntities[pointIndex] = pointEntity;
116118
}
117119

@@ -152,11 +154,14 @@ void CurveEditor::updateCurves( bool onRelease ) {
152154
}
153155

154156
Transform CurveEditor::computePointTransform( PointComponent* pointCmp,
155-
const Vector3& midPoint,
157+
PointComponent* midPointCmp,
156158
const Vector3& worldPos ) {
157159
auto transform = Transform::Identity();
160+
auto midPoint = midPointCmp->m_point;
158161
Vector3 diff = ( midPoint - worldPos );
159-
if ( m_symetry ) { transform.translate( ( midPoint + diff ) - pointCmp->m_defaultPoint ); }
162+
if ( midPointCmp->m_state == PointComponent::SYMETRIC ) {
163+
transform.translate( ( midPoint + diff ) - pointCmp->m_defaultPoint );
164+
}
160165
else {
161166
diff.normalize();
162167
auto actualdiff = diff * ( midPoint - pointCmp->m_point ).norm();
@@ -335,6 +340,12 @@ bool CurveEditor::processHover( std::shared_ptr<Rendering::RenderObject> ro ) {
335340
}
336341
if ( m_currentPoint < 0 ) return false;
337342

343+
if ( m_currentPoint % 3 == 0 )
344+
m_savedPoint = m_currentPoint;
345+
else if ( m_savedPoint >= 0 && m_savedPoint - 1 != m_currentPoint &&
346+
m_savedPoint + 1 != m_currentPoint )
347+
m_savedPoint = -1;
348+
338349
ro->getMaterial()->getParameters().addParameter( "material.color", Color::Red() );
339350
m_selectedRo = ro;
340351
return true;
@@ -379,22 +390,22 @@ void CurveEditor::processPicking( const Vector3& worldPos ) {
379390
m_tangentPoints.push_back( m_currentPoint + 1 );
380391
}
381392
}
382-
else if ( m_smooth ) {
383-
if ( m_currentPoint % 3 == 2 ) {
384-
auto pointMid = m_pointEntities[m_currentPoint + 1];
385-
pointComponent = m_pointEntities[m_currentPoint + 2];
386-
auto symTransform =
387-
computePointTransform( pointComponent, pointMid->m_point, worldPos );
393+
else if ( m_currentPoint % 3 == 2 ) {
394+
auto pointMid = m_pointEntities[m_currentPoint + 1];
395+
if ( !( pointMid->m_state == PointComponent::DEFAULT ) ) {
396+
pointComponent = m_pointEntities[m_currentPoint + 2];
397+
auto symTransform = computePointTransform( pointComponent, pointMid, worldPos );
388398
auto ro = m_roMgr->getRenderObject( pointComponent->getRenderObjects()[0] );
389399
ro->setLocalTransform( symTransform );
390400

391401
if ( m_tangentPoints.empty() ) { m_tangentPoints.push_back( m_currentPoint + 2 ); }
392402
}
393-
else if ( m_currentPoint % 3 == 1 ) {
394-
auto pointMid = m_pointEntities[m_currentPoint - 1];
395-
pointComponent = m_pointEntities[m_currentPoint - 2];
396-
auto symTransform =
397-
computePointTransform( pointComponent, pointMid->m_point, worldPos );
403+
}
404+
else if ( m_currentPoint % 3 == 1 ) {
405+
auto pointMid = m_pointEntities[m_currentPoint - 1];
406+
if ( !( pointMid->m_state == PointComponent::DEFAULT ) ) {
407+
pointComponent = m_pointEntities[m_currentPoint - 2];
408+
auto symTransform = computePointTransform( pointComponent, pointMid, worldPos );
398409
auto ro = m_roMgr->getRenderObject( pointComponent->getRenderObjects()[0] );
399410
ro->setLocalTransform( symTransform );
400411

@@ -404,3 +415,112 @@ void CurveEditor::processPicking( const Vector3& worldPos ) {
404415
}
405416
updateCurves();
406417
}
418+
419+
void CurveEditor::setSmooth( bool smooth ) {
420+
if ( m_savedPoint >= 0 ) {
421+
if ( smooth ) {
422+
m_pointEntities[m_savedPoint]->m_state = PointComponent::SMOOTH;
423+
smoothify( m_savedPoint );
424+
}
425+
else
426+
m_pointEntities[m_savedPoint]->m_state = PointComponent::DEFAULT;
427+
}
428+
}
429+
430+
void CurveEditor::setSymetry( bool symetry ) {
431+
if ( m_savedPoint >= 0 ) {
432+
if ( symetry ) {
433+
m_pointEntities[m_savedPoint]->m_state = PointComponent::SYMETRIC;
434+
symmetrize( m_savedPoint );
435+
}
436+
else
437+
m_pointEntities[m_savedPoint]->m_state = PointComponent::SMOOTH;
438+
}
439+
}
440+
441+
void CurveEditor::smoothify( int pointId ) {
442+
if ( ( pointId == 0 || pointId == 1 || pointId == m_pointEntities.size() - 2 ||
443+
pointId == m_pointEntities.size() - 1 ) )
444+
return;
445+
446+
m_viewer->makeCurrent();
447+
Vector3 backPoint = m_pointEntities[pointId - 1]->m_point;
448+
Vector3 back = backPoint - m_pointEntities[pointId]->m_point;
449+
Vector3 frontPoint = m_pointEntities[pointId + 1]->m_point;
450+
Vector3 front = frontPoint - m_pointEntities[pointId]->m_point;
451+
452+
Vector3 newPointDir = back - front;
453+
newPointDir.normalize();
454+
newPointDir = newPointDir * back.norm();
455+
Vector3 newBackPoint = m_pointEntities[pointId]->m_point + newPointDir;
456+
457+
auto transform = Transform::Identity();
458+
transform.translate( newBackPoint - m_pointEntities[pointId - 1]->m_point );
459+
auto roIdx = m_pointEntities[pointId - 1]->getRenderObjects()[0];
460+
auto ro = m_roMgr->getRenderObject( roIdx );
461+
ro->setLocalTransform( transform );
462+
m_pointEntities[pointId - 1]->m_point = newBackPoint;
463+
464+
newPointDir.normalize();
465+
newPointDir = newPointDir * front.norm();
466+
Vector3 newFrontPoint = m_pointEntities[pointId]->m_point - newPointDir;
467+
468+
transform = Transform::Identity();
469+
transform.translate( newFrontPoint - m_pointEntities[pointId + 1]->m_point );
470+
roIdx = m_pointEntities[pointId + 1]->getRenderObjects()[0];
471+
ro = m_roMgr->getRenderObject( roIdx );
472+
ro->setLocalTransform( transform );
473+
m_pointEntities[pointId + 1]->m_point = newFrontPoint;
474+
475+
m_currentPoint = pointId - 1;
476+
updateCurves( true );
477+
m_currentPoint = pointId + 1;
478+
updateCurves( true );
479+
m_currentPoint = -1;
480+
481+
m_viewer->doneCurrent();
482+
m_viewer->getRenderer()->buildAllRenderTechniques();
483+
m_viewer->needUpdate();
484+
}
485+
486+
void CurveEditor::symmetrize( int pointId ) {
487+
if ( ( pointId == 0 || pointId == 1 || pointId == m_pointEntities.size() - 2 ||
488+
pointId == m_pointEntities.size() - 1 ) )
489+
return;
490+
491+
m_viewer->makeCurrent();
492+
493+
Vector3 backPoint = m_pointEntities[pointId - 1]->m_point;
494+
Vector3 back = backPoint - m_pointEntities[pointId]->m_point;
495+
Vector3 frontPoint = m_pointEntities[pointId + 1]->m_point;
496+
Vector3 front = frontPoint - m_pointEntities[pointId]->m_point;
497+
498+
auto transform = Transform::Identity();
499+
Index roIdx = -1;
500+
if ( std::min( back.norm(), front.norm() ) == back.norm() ) {
501+
front.normalize();
502+
front = front * back.norm();
503+
Vector3 newPoint = m_pointEntities[pointId]->m_point + front;
504+
transform.translate( newPoint - m_pointEntities[pointId + 1]->m_point );
505+
roIdx = m_pointEntities[pointId + 1]->getRenderObjects()[0];
506+
m_pointEntities[pointId + 1]->m_point = newPoint;
507+
m_currentPoint = pointId + 1;
508+
}
509+
else {
510+
back.normalize();
511+
back = back * front.norm();
512+
Vector3 newPoint = m_pointEntities[pointId]->m_point + back;
513+
transform.translate( newPoint - m_pointEntities[pointId - 1]->m_point );
514+
roIdx = m_pointEntities[pointId - 1]->getRenderObjects()[0];
515+
m_pointEntities[pointId - 1]->m_point = newPoint;
516+
m_currentPoint = pointId - 1;
517+
}
518+
auto ro = m_roMgr->getRenderObject( roIdx );
519+
ro->setLocalTransform( transform );
520+
updateCurves( true );
521+
m_currentPoint = -1;
522+
523+
m_viewer->doneCurrent();
524+
m_viewer->getRenderer()->buildAllRenderTechniques();
525+
m_viewer->needUpdate();
526+
}

examples/CurveEditor/CurveEditor.hpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,54 @@ class CurveEditor : public Ra::Engine::Scene::Entity
5858
m_currentPoint = -1;
5959
}
6060

61+
void resetSavedPoint() { m_savedPoint = -1; }
62+
63+
PointComponent* getSavedPoint() {
64+
if ( m_savedPoint >= 0 )
65+
return m_pointEntities[m_savedPoint];
66+
else
67+
return nullptr;
68+
}
69+
6170
/**
6271
* @brief smooth to keep a G1 continuity
6372
* @param smooth state
6473
*/
65-
void setSmooth( bool smooth ) { m_smooth = smooth; }
74+
void setSmooth( bool smooth );
6675

6776
/**
6877
* @brief symetry to keep a C1 continuity
6978
* @param symetry state
7079
*/
71-
void setSymetry( bool symetry ) { m_symetry = symetry; }
80+
void setSymetry( bool symetry );
7281

7382
private:
7483
inline float distanceSquared( const Ra::Core::Vector3f& pointA,
7584
const Ra::Core::Vector3f& pointB );
85+
7686
unsigned int getPointIndex( unsigned int curveIdSize,
7787
unsigned int currentPtIndex,
7888
const Ra::Core::Vector3& firstCtrlPt,
7989
const Ra::Core::Vector3& currentPt );
90+
8091
void updateCurve( unsigned int curveId,
8192
unsigned int curveIdSize,
8293
PointComponent* pointComponent,
8394
const Ra::Core::Vector3& currentPt,
8495
unsigned int currentPoint );
96+
8597
Ra::Core::Transform computePointTransform( PointComponent* pointCmp,
86-
const Ra::Core::Vector3& midPoint,
98+
PointComponent* midPoint,
8799
const Ra::Core::Vector3& worldPos );
88100
void subdivisionBezier( int vertexIndex,
89101
unsigned int curveIndex,
90102
const Ra::Core::Vector3Array& ctrlPts );
91103
void refreshPoint( unsigned int pointIndex,
92104
const Ra::Core::Vector3& newPoint = Ra::Core::Vector3::Zero() );
93105

106+
void smoothify( int pointId );
107+
void symmetrize( int pointId );
108+
94109
private:
95110
Ra::Engine::Scene::EntityManager* m_entityMgr;
96111
int m_currentPoint { -1 };
@@ -99,6 +114,7 @@ class CurveEditor : public Ra::Engine::Scene::Entity
99114
std::vector<CurveComponent*> m_curveEntities;
100115
std::vector<unsigned int> m_tangentPoints;
101116
Ra::Engine::Rendering::RenderObjectManager* m_roMgr;
117+
int m_savedPoint { -1 };
102118

103119
bool m_smooth { false };
104120
bool m_symetry { false };

examples/CurveEditor/Gui/MainWindow.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ MainWindow::MainWindow( QWidget* parent ) : MainWindowInterface( parent ) {
5656
m_symetryButton = new QPushButton( "Symetry" );
5757
m_symetryButton->setCheckable( true );
5858
m_symetryButton->setEnabled( false );
59+
m_button->setEnabled( false );
5960
layout->addWidget( m_hidePolylineButton );
6061
layout->addWidget( m_editCurveButton );
6162
layout->addWidget( m_button );
@@ -105,10 +106,10 @@ void MainWindow::cleanup() {
105106
}
106107

107108
void MainWindow::createConnections() {
108-
connect( getViewer(), &MyViewer::toggleBrushPicking, this, &MainWindow::toggleCirclePicking );
109-
connect( getViewer(), &MyViewer::onMouseMove, this, &MainWindow::handleMouseMoveEvent );
110-
connect( getViewer(), &MyViewer::onMouseRelease, this, &MainWindow::handleMouseReleaseEvent );
111-
connect( getViewer(), &MyViewer::onMousePress, this, &MainWindow::handleMousePressEvent );
109+
connect( m_viewer, &MyViewer::toggleBrushPicking, this, &MainWindow::toggleCirclePicking );
110+
connect( m_viewer, &MyViewer::onMouseMove, this, &MainWindow::handleMouseMoveEvent );
111+
connect( m_viewer, &MyViewer::onMouseRelease, this, &MainWindow::handleMouseReleaseEvent );
112+
connect( m_viewer, &MyViewer::onMousePress, this, &MainWindow::handleMousePressEvent );
112113
connect(
113114
m_editCurveButton, &QPushButton::pressed, this, &MainWindow::onEditPolylineButtonPressed );
114115
connect( m_hidePolylineButton,
@@ -117,15 +118,13 @@ void MainWindow::createConnections() {
117118
&MainWindow::onHidePolylineButtonClicked );
118119
connect( m_button, &QPushButton::clicked, this, &MainWindow::onSmoothButtonClicked );
119120
connect( m_symetryButton, &QPushButton::clicked, this, &MainWindow::onSymetryButtonClicked );
120-
connect( getViewer(),
121-
&MyViewer::onMouseDoubleClick,
122-
this,
123-
&MainWindow::handleMouseDoubleClickEvent );
121+
connect(
122+
m_viewer, &MyViewer::onMouseDoubleClick, this, &MainWindow::handleMouseDoubleClickEvent );
124123
}
125124

126125
void MainWindow::onSmoothButtonClicked() {
127-
m_symetryButton->setEnabled( m_button->isChecked() );
128126
m_curveEditor->setSmooth( m_button->isChecked() );
127+
m_symetryButton->setEnabled( true );
129128
}
130129

131130
void MainWindow::onSymetryButtonClicked() {
@@ -148,8 +147,41 @@ void MainWindow::onEditPolylineButtonPressed() {
148147
m_curveEditor = new CurveEditor( m_polyline, m_viewer );
149148
}
150149

150+
void MainWindow::processSavedPoint() {
151+
auto savedPoint = m_curveEditor->getSavedPoint();
152+
if ( savedPoint ) {
153+
if ( savedPoint->m_state == PointComponent::DEFAULT ) {
154+
m_button->setChecked( false );
155+
m_button->setEnabled( true );
156+
m_symetryButton->setEnabled( false );
157+
m_symetryButton->setChecked( false );
158+
}
159+
else if ( savedPoint->m_state == PointComponent::SMOOTH ) {
160+
m_button->setEnabled( true );
161+
m_button->setChecked( true );
162+
m_symetryButton->setEnabled( true );
163+
m_symetryButton->setChecked( false );
164+
}
165+
else {
166+
m_button->setEnabled( true );
167+
m_button->setChecked( true );
168+
m_symetryButton->setEnabled( true );
169+
m_symetryButton->setChecked( true );
170+
}
171+
}
172+
else {
173+
m_button->setEnabled( false );
174+
m_button->setChecked( false );
175+
m_symetryButton->setEnabled( false );
176+
m_symetryButton->setChecked( false );
177+
}
178+
}
179+
151180
void MainWindow::handleMousePressEvent( QMouseEvent* event ) {
152-
if ( event->button() == Qt::RightButton ) m_clicked = true;
181+
if ( m_edited ) {
182+
if ( event->button() == Qt::RightButton ) m_clicked = true;
183+
processSavedPoint();
184+
}
153185
}
154186

155187
void MainWindow::displayHelpDialog() {

examples/CurveEditor/Gui/MainWindow.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class MainWindow : public Ra::Gui::MainWindowInterface
6161

6262
private:
6363
void createConnections();
64+
65+
void processSavedPoint();
66+
6467
MyViewer* m_viewer;
6568
Ra::Gui::SelectionManager* m_selectionManager;
6669
Ra::Gui::ItemModel* m_sceneModel;

examples/CurveEditor/PointComponent.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class PointComponent : public Ra::Engine::Scene::Component
1919
/// setup, i.e. it has an entity.
2020
void initialize() override;
2121

22+
enum State { DEFAULT = 0, SMOOTH, SYMETRIC };
23+
24+
State m_state { DEFAULT };
2225
Ra::Core::Vector3 m_point;
2326
Ra::Core::Vector3 m_defaultPoint;
2427
Ra::Core::Utils::Color m_color;

0 commit comments

Comments
 (0)