| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_CURVE_H ) |
| 23 |
#define INCLUDED_CURVE_H |
| 24 |
|
| 25 |
#include "ientity.h" |
| 26 |
#include "selectable.h" |
| 27 |
#include "renderable.h" |
| 28 |
|
| 29 |
#include <set> |
| 30 |
|
| 31 |
#include "math/curve.h" |
| 32 |
#include "stream/stringstream.h" |
| 33 |
#include "signal/signal.h" |
| 34 |
#include "selectionlib.h" |
| 35 |
#include "render.h" |
| 36 |
#include "stringio.h" |
| 37 |
|
| 38 |
class RenderableCurve : public OpenGLRenderable |
| 39 |
{ |
| 40 |
public: |
| 41 |
std::vector<PointVertex> m_vertices; |
| 42 |
void render( RenderStateFlags state ) const { |
| 43 |
pointvertex_gl_array( &m_vertices.front() ); |
| 44 |
glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) ); |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
inline void plotBasisFunction( std::size_t numSegments, int point, int degree ){ |
| 49 |
Knots knots; |
| 50 |
KnotVector_openUniform( knots, 4, degree ); |
| 51 |
|
| 52 |
globalOutputStream() << "plotBasisFunction point " << point << " of 4, knot vector:"; |
| 53 |
for ( Knots::iterator i = knots.begin(); i != knots.end(); ++i ) |
| 54 |
{ |
| 55 |
globalOutputStream() << " " << *i; |
| 56 |
} |
| 57 |
globalOutputStream() << "\n"; |
| 58 |
globalOutputStream() << "t=0 basis=" << BSpline_basis( knots, point, degree, 0.0 ) << "\n"; |
| 59 |
for ( std::size_t i = 1; i < numSegments; ++i ) |
| 60 |
{ |
| 61 |
double t = ( 1.0 / double(numSegments) ) * double(i); |
| 62 |
globalOutputStream() << "t=" << t << " basis=" << BSpline_basis( knots, point, degree, t ) << "\n"; |
| 63 |
} |
| 64 |
globalOutputStream() << "t=1 basis=" << BSpline_basis( knots, point, degree, 1.0 ) << "\n"; |
| 65 |
} |
| 66 |
|
| 67 |
inline bool ControlPoints_parse( ControlPoints& controlPoints, const char* value ){ |
| 68 |
StringTokeniser tokeniser( value, " " ); |
| 69 |
|
| 70 |
std::size_t size; |
| 71 |
if ( !string_parse_size( tokeniser.getToken(), size ) ) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
if ( size < 3 ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
controlPoints.resize( size ); |
| 79 |
|
| 80 |
if ( !string_equal( tokeniser.getToken(), "(" ) ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
for ( ControlPoints::iterator i = controlPoints.begin(); i != controlPoints.end(); ++i ) |
| 84 |
{ |
| 85 |
if ( !string_parse_float( tokeniser.getToken(), ( *i ).x() ) |
| 86 |
|| !string_parse_float( tokeniser.getToken(), ( *i ).y() ) |
| 87 |
|| !string_parse_float( tokeniser.getToken(), ( *i ).z() ) ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
} |
| 91 |
if ( !string_equal( tokeniser.getToken(), ")" ) ) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
inline void ControlPoints_write( const ControlPoints& controlPoints, StringOutputStream& value ){ |
| 98 |
value << Unsigned( controlPoints.size() ) << " ("; |
| 99 |
for ( ControlPoints::const_iterator i = controlPoints.begin(); i != controlPoints.end(); ++i ) |
| 100 |
{ |
| 101 |
value << " " << ( *i ).x() << " " << ( *i ).y() << " " << ( *i ).z() << " "; |
| 102 |
} |
| 103 |
value << ")"; |
| 104 |
} |
| 105 |
|
| 106 |
inline void ControlPoint_testSelect( const Vector3& point, ObservedSelectable& selectable, Selector& selector, SelectionTest& test ){ |
| 107 |
SelectionIntersection best; |
| 108 |
test.TestPoint( point, best ); |
| 109 |
if ( best.valid() ) { |
| 110 |
Selector_add( selector, selectable, best ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
class CurveEditType |
| 115 |
{ |
| 116 |
public: |
| 117 |
Shader* m_controlsShader; |
| 118 |
Shader* m_selectedShader; |
| 119 |
}; |
| 120 |
|
| 121 |
inline void ControlPoints_write( ControlPoints& controlPoints, const char* key, Entity& entity ){ |
| 122 |
StringOutputStream value( 256 ); |
| 123 |
if ( !controlPoints.empty() ) { |
| 124 |
ControlPoints_write( controlPoints, value ); |
| 125 |
} |
| 126 |
entity.setKeyValue( key, value.c_str() ); |
| 127 |
} |
| 128 |
|
| 129 |
class CurveEdit |
| 130 |
{ |
| 131 |
SelectionChangeCallback m_selectionChanged; |
| 132 |
ControlPoints& m_controlPoints; |
| 133 |
typedef Array<ObservedSelectable> Selectables; |
| 134 |
Selectables m_selectables; |
| 135 |
|
| 136 |
RenderablePointVector m_controlsRender; |
| 137 |
mutable RenderablePointVector m_selectedRender; |
| 138 |
|
| 139 |
public: |
| 140 |
typedef Static<CurveEditType> Type; |
| 141 |
|
| 142 |
CurveEdit( ControlPoints& controlPoints, const SelectionChangeCallback& selectionChanged ) : |
| 143 |
m_selectionChanged( selectionChanged ), |
| 144 |
m_controlPoints( controlPoints ), |
| 145 |
m_controlsRender( GL_POINTS ), |
| 146 |
m_selectedRender( GL_POINTS ){ |
| 147 |
} |
| 148 |
|
| 149 |
template<typename Functor> |
| 150 |
const Functor& forEachSelected( const Functor& functor ){ |
| 151 |
ASSERT_MESSAGE( m_controlPoints.size() == m_selectables.size(), "curve instance mismatch" ); |
| 152 |
ControlPoints::iterator p = m_controlPoints.begin(); |
| 153 |
for ( Selectables::iterator i = m_selectables.begin(); i != m_selectables.end(); ++i, ++p ) |
| 154 |
{ |
| 155 |
if ( ( *i ).isSelected() ) { |
| 156 |
functor( *p ); |
| 157 |
} |
| 158 |
} |
| 159 |
return functor; |
| 160 |
} |
| 161 |
template<typename Functor> |
| 162 |
const Functor& forEachSelected( const Functor& functor ) const { |
| 163 |
ASSERT_MESSAGE( m_controlPoints.size() == m_selectables.size(), "curve instance mismatch" ); |
| 164 |
ControlPoints::const_iterator p = m_controlPoints.begin(); |
| 165 |
for ( Selectables::const_iterator i = m_selectables.begin(); i != m_selectables.end(); ++i, ++p ) |
| 166 |
{ |
| 167 |
if ( ( *i ).isSelected() ) { |
| 168 |
functor( *p ); |
| 169 |
} |
| 170 |
} |
| 171 |
return functor; |
| 172 |
} |
| 173 |
template<typename Functor> |
| 174 |
const Functor& forEach( const Functor& functor ) const { |
| 175 |
for ( ControlPoints::const_iterator i = m_controlPoints.begin(); i != m_controlPoints.end(); ++i ) |
| 176 |
{ |
| 177 |
functor( *i ); |
| 178 |
} |
| 179 |
return functor; |
| 180 |
} |
| 181 |
|
| 182 |
void testSelect( Selector& selector, SelectionTest& test ){ |
| 183 |
ASSERT_MESSAGE( m_controlPoints.size() == m_selectables.size(), "curve instance mismatch" ); |
| 184 |
ControlPoints::const_iterator p = m_controlPoints.begin(); |
| 185 |
for ( Selectables::iterator i = m_selectables.begin(); i != m_selectables.end(); ++i, ++p ) |
| 186 |
{ |
| 187 |
ControlPoint_testSelect( *p, *i, selector, test ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
bool isSelected() const { |
| 192 |
for ( Selectables::const_iterator i = m_selectables.begin(); i != m_selectables.end(); ++i ) |
| 193 |
{ |
| 194 |
if ( ( *i ).isSelected() ) { |
| 195 |
return true; |
| 196 |
} |
| 197 |
} |
| 198 |
return false; |
| 199 |
} |
| 200 |
void setSelected( bool selected ){ |
| 201 |
for ( Selectables::iterator i = m_selectables.begin(); i != m_selectables.end(); ++i ) |
| 202 |
{ |
| 203 |
( *i ).setSelected( selected ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
void write( const char* key, Entity& entity ){ |
| 208 |
ControlPoints_write( m_controlPoints, key, entity ); |
| 209 |
} |
| 210 |
|
| 211 |
void transform( const Matrix4& matrix ){ |
| 212 |
forEachSelected([&](Vector3 &point) { |
| 213 |
matrix4_transform_point(matrix, point); |
| 214 |
}); |
| 215 |
} |
| 216 |
void snapto( float snap ){ |
| 217 |
forEachSelected([&](Vector3 &point) { |
| 218 |
vector3_snap(point, snap); |
| 219 |
}); |
| 220 |
} |
| 221 |
|
| 222 |
void updateSelected() const { |
| 223 |
m_selectedRender.clear(); |
| 224 |
forEachSelected([&](const Vector3 &point) { |
| 225 |
m_selectedRender.push_back(PointVertex(vertex3f_for_vector3(point), colour_selected)); |
| 226 |
}); |
| 227 |
} |
| 228 |
|
| 229 |
void renderComponents( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const { |
| 230 |
renderer.SetState( Type::instance().m_controlsShader, Renderer::eWireframeOnly ); |
| 231 |
renderer.SetState( Type::instance().m_controlsShader, Renderer::eFullMaterials ); |
| 232 |
renderer.addRenderable( m_controlsRender, localToWorld ); |
| 233 |
} |
| 234 |
|
| 235 |
void renderComponentsSelected( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const { |
| 236 |
updateSelected(); |
| 237 |
if ( !m_selectedRender.empty() ) { |
| 238 |
renderer.Highlight( Renderer::ePrimitive, false ); |
| 239 |
renderer.SetState( Type::instance().m_selectedShader, Renderer::eWireframeOnly ); |
| 240 |
renderer.SetState( Type::instance().m_selectedShader, Renderer::eFullMaterials ); |
| 241 |
renderer.addRenderable( m_selectedRender, localToWorld ); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
void curveChanged(){ |
| 246 |
m_selectables.resize( m_controlPoints.size(), m_selectionChanged ); |
| 247 |
|
| 248 |
m_controlsRender.clear(); |
| 249 |
m_controlsRender.reserve( m_controlPoints.size() ); |
| 250 |
forEach([&](const Vector3 &point) { |
| 251 |
m_controlsRender.push_back(PointVertex(vertex3f_for_vector3(point), colour_vertex)); |
| 252 |
}); |
| 253 |
|
| 254 |
m_selectedRender.reserve( m_controlPoints.size() ); |
| 255 |
} |
| 256 |
typedef MemberCaller<CurveEdit, void(), &CurveEdit::curveChanged> CurveChangedCaller; |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
const int NURBS_degree = 3; |
| 262 |
|
| 263 |
class NURBSCurve |
| 264 |
{ |
| 265 |
Signal0 m_curveChanged; |
| 266 |
Callback<void()> m_boundsChanged; |
| 267 |
public: |
| 268 |
ControlPoints m_controlPoints; |
| 269 |
ControlPoints m_controlPointsTransformed; |
| 270 |
NURBSWeights m_weights; |
| 271 |
Knots m_knots; |
| 272 |
RenderableCurve m_renderCurve; |
| 273 |
AABB m_bounds; |
| 274 |
|
| 275 |
NURBSCurve( const Callback<void()>& boundsChanged ) : m_boundsChanged( boundsChanged ){ |
| 276 |
} |
| 277 |
|
| 278 |
SignalHandlerId connect( const SignalHandler& curveChanged ){ |
| 279 |
curveChanged(); |
| 280 |
return m_curveChanged.connectLast( curveChanged ); |
| 281 |
} |
| 282 |
void disconnect( SignalHandlerId id ){ |
| 283 |
m_curveChanged.disconnect( id ); |
| 284 |
} |
| 285 |
void notify(){ |
| 286 |
m_curveChanged(); |
| 287 |
} |
| 288 |
|
| 289 |
void tesselate(){ |
| 290 |
if ( !m_controlPointsTransformed.empty() ) { |
| 291 |
const std::size_t numSegments = ( m_controlPointsTransformed.size() - 1 ) * 16; |
| 292 |
m_renderCurve.m_vertices.resize( numSegments + 1 ); |
| 293 |
m_renderCurve.m_vertices[0].vertex = vertex3f_for_vector3( m_controlPointsTransformed[0] ); |
| 294 |
for ( std::size_t i = 1; i < numSegments; ++i ) |
| 295 |
{ |
| 296 |
m_renderCurve.m_vertices[i].vertex = vertex3f_for_vector3( NURBS_evaluate( m_controlPointsTransformed, m_weights, m_knots, NURBS_degree, ( 1.0 / double(numSegments) ) * double(i) ) ); |
| 297 |
} |
| 298 |
m_renderCurve.m_vertices[numSegments].vertex = vertex3f_for_vector3( m_controlPointsTransformed[m_controlPointsTransformed.size() - 1] ); |
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
m_renderCurve.m_vertices.clear(); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
void curveChanged(){ |
| 307 |
tesselate(); |
| 308 |
|
| 309 |
m_bounds = AABB(); |
| 310 |
for ( ControlPoints::iterator i = m_controlPointsTransformed.begin(); i != m_controlPointsTransformed.end(); ++i ) |
| 311 |
{ |
| 312 |
aabb_extend_by_point_safe( m_bounds, ( *i ) ); |
| 313 |
} |
| 314 |
|
| 315 |
m_boundsChanged(); |
| 316 |
notify(); |
| 317 |
} |
| 318 |
|
| 319 |
bool parseCurve( const char* value ){ |
| 320 |
if ( !ControlPoints_parse( m_controlPoints, value ) ) { |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
m_weights.resize( m_controlPoints.size() ); |
| 325 |
for ( NURBSWeights::iterator i = m_weights.begin(); i != m_weights.end(); ++i ) |
| 326 |
{ |
| 327 |
( *i ) = 1; |
| 328 |
} |
| 329 |
|
| 330 |
KnotVector_openUniform( m_knots, m_controlPoints.size(), NURBS_degree ); |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
return true; |
| 335 |
} |
| 336 |
|
| 337 |
void curveChanged( const char* value ){ |
| 338 |
if ( string_empty( value ) || !parseCurve( value ) ) { |
| 339 |
m_controlPoints.resize( 0 ); |
| 340 |
m_knots.resize( 0 ); |
| 341 |
m_weights.resize( 0 ); |
| 342 |
} |
| 343 |
m_controlPointsTransformed = m_controlPoints; |
| 344 |
curveChanged(); |
| 345 |
} |
| 346 |
typedef MemberCaller<NURBSCurve, void(const char*), &NURBSCurve::curveChanged> CurveChangedCaller; |
| 347 |
}; |
| 348 |
|
| 349 |
class CatmullRomSpline |
| 350 |
{ |
| 351 |
Signal0 m_curveChanged; |
| 352 |
Callback<void()> m_boundsChanged; |
| 353 |
public: |
| 354 |
ControlPoints m_controlPoints; |
| 355 |
ControlPoints m_controlPointsTransformed; |
| 356 |
RenderableCurve m_renderCurve; |
| 357 |
AABB m_bounds; |
| 358 |
|
| 359 |
CatmullRomSpline( const Callback<void()>& boundsChanged ) : m_boundsChanged( boundsChanged ){ |
| 360 |
} |
| 361 |
|
| 362 |
SignalHandlerId connect( const SignalHandler& curveChanged ){ |
| 363 |
curveChanged(); |
| 364 |
return m_curveChanged.connectLast( curveChanged ); |
| 365 |
} |
| 366 |
void disconnect( SignalHandlerId id ){ |
| 367 |
m_curveChanged.disconnect( id ); |
| 368 |
} |
| 369 |
void notify(){ |
| 370 |
m_curveChanged(); |
| 371 |
} |
| 372 |
|
| 373 |
void tesselate(){ |
| 374 |
if ( !m_controlPointsTransformed.empty() ) { |
| 375 |
const std::size_t numSegments = ( m_controlPointsTransformed.size() - 1 ) * 16; |
| 376 |
m_renderCurve.m_vertices.resize( numSegments + 1 ); |
| 377 |
m_renderCurve.m_vertices[0].vertex = vertex3f_for_vector3( m_controlPointsTransformed[0] ); |
| 378 |
for ( std::size_t i = 1; i < numSegments; ++i ) |
| 379 |
{ |
| 380 |
m_renderCurve.m_vertices[i].vertex = vertex3f_for_vector3( CatmullRom_evaluate( m_controlPointsTransformed, ( 1.0 / double(numSegments) ) * double(i) ) ); |
| 381 |
} |
| 382 |
m_renderCurve.m_vertices[numSegments].vertex = vertex3f_for_vector3( m_controlPointsTransformed[m_controlPointsTransformed.size() - 1] ); |
| 383 |
} |
| 384 |
else |
| 385 |
{ |
| 386 |
m_renderCurve.m_vertices.clear(); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
bool parseCurve( const char* value ){ |
| 391 |
return ControlPoints_parse( m_controlPoints, value ); |
| 392 |
} |
| 393 |
|
| 394 |
void curveChanged(){ |
| 395 |
tesselate(); |
| 396 |
|
| 397 |
m_bounds = AABB(); |
| 398 |
for ( ControlPoints::iterator i = m_controlPointsTransformed.begin(); i != m_controlPointsTransformed.end(); ++i ) |
| 399 |
{ |
| 400 |
aabb_extend_by_point_safe( m_bounds, ( *i ) ); |
| 401 |
} |
| 402 |
|
| 403 |
m_boundsChanged(); |
| 404 |
notify(); |
| 405 |
} |
| 406 |
|
| 407 |
void curveChanged( const char* value ){ |
| 408 |
if ( string_empty( value ) || !parseCurve( value ) ) { |
| 409 |
m_controlPoints.resize( 0 ); |
| 410 |
} |
| 411 |
m_controlPointsTransformed = m_controlPoints; |
| 412 |
curveChanged(); |
| 413 |
} |
| 414 |
typedef MemberCaller<CatmullRomSpline, void(const char*), &CatmullRomSpline::curveChanged> CurveChangedCaller; |
| 415 |
}; |
| 416 |
|
| 417 |
const char* const curve_Nurbs = "curve_Nurbs"; |
| 418 |
const char* const curve_CatmullRomSpline = "curve_CatmullRomSpline"; |
| 419 |
|
| 420 |
|
| 421 |
#endif |