PrimitivesGenerator.cc 21.6 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/



#include "PrimitivesGenerator.hh"


#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

Jan Möbius's avatar
Jan Möbius committed
50 51
PrimitivesGeneratorPlugin::PrimitivesGeneratorPlugin() :
        triMesh_(0),
52 53 54
        polyMesh_(0),
        slices_(50),
        stacks_(50)
Jan Möbius's avatar
Jan Möbius committed
55 56 57 58 59 60 61 62 63
{

}

PrimitivesGeneratorPlugin::~PrimitivesGeneratorPlugin()
{

}

Jan Möbius's avatar
Jan Möbius committed
64 65 66 67 68 69
void PrimitivesGeneratorPlugin::initializePlugin()
{
  emit setSlotDescription("addTetrahedron()"     ,tr("Generates a tetrahedron (ObjectId is returned)")  ,QStringList(), QStringList());
  emit setSlotDescription("addTriangulatedCube()",tr("Generates a cube (ObjectId is returned)")         ,QStringList(), QStringList());
  emit setSlotDescription("addIcosahedron()"     ,tr("Generates an icosahedron (ObjectId is returned)") ,QStringList(), QStringList());
  emit setSlotDescription("addPyramid()"         ,tr("Generates a pyramid (ObjectId is returned)")      ,QStringList(), QStringList());
Jan Möbius's avatar
Jan Möbius committed
70
  emit setSlotDescription("addOctahedron()"      ,tr("Generates an octahedron (ObjectId is returned)")  ,QStringList(), QStringList());
71 72
  emit setSlotDescription("addDodecahedron()"    ,tr("Generates a dodecahedron (ObjectId is returned)") ,QStringList(), QStringList());
  emit setSlotDescription("addSphere()"          ,tr("Generates a sphere (ObjectId is returned)")       ,QStringList(), QStringList());
Jan Möbius's avatar
Jan Möbius committed
73 74 75 76
}

void PrimitivesGeneratorPlugin::pluginsInitialized() {

Jan Möbius's avatar
Jan Möbius committed
77 78 79
  if ( OpenFlipper::Options::gui()) {
    // Primitives Menu
    QMenu *primitivesMenu;
Jan Möbius's avatar
Jan Möbius committed
80

Jan Möbius's avatar
Jan Möbius committed
81
    emit getMenubarMenu(tr("&Primitives"), primitivesMenu, true );
Jan Möbius's avatar
Jan Möbius committed
82

Jan Möbius's avatar
Jan Möbius committed
83
    QIcon* icon;
Matthias Möller's avatar
Matthias Möller committed
84
    QAction* action;
Jan Möbius's avatar
Jan Möbius committed
85

Jan Möbius's avatar
Jan Möbius committed
86 87 88
    action = primitivesMenu->addAction("Cube (Triangle Mesh)"       ,this,SLOT(addTriangulatedCube()));
    icon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"primitive_cube.png");
    action->setIcon(*icon);
89

90 91
    WhatsThisGenerator whatsThisGen("PrimitivesGenerator");
    whatsThisGen.setWhatsThis(action,tr("Create a Cube."),"Cube");
Jan Möbius's avatar
Jan Möbius committed
92

93
    action = primitivesMenu->addAction("Dodecahedron"               ,this,SLOT(addDodecahedron()));
94
    icon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"primitive_dodecahedron.png");
Jan Möbius's avatar
Jan Möbius committed
95
    action->setIcon(*icon);
96
    whatsThisGen.setWhatsThis(action,tr("Create a Dodecahedron."), "Dodecahedron");
Jan Möbius's avatar
Jan Möbius committed
97

Jan Möbius's avatar
Jan Möbius committed
98 99 100
    action = primitivesMenu->addAction("Icosahedron"                ,this,SLOT(addIcosahedron()));
    icon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"primitive_icosahedron.png");
    action->setIcon(*icon);
101
    whatsThisGen.setWhatsThis(action,tr("Create a Icosahedron.","Icosahedron"));
Jan Möbius's avatar
Jan Möbius committed
102

103 104 105
    action = primitivesMenu->addAction("Octahedron"                  ,this,SLOT(addOctahedron()));
    icon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"primitive_octahedron.png");
    action->setIcon(*icon);
106
    whatsThisGen.setWhatsThis(action,tr("Create an Octahedron."),"Octahedron");
Jan Möbius's avatar
Jan Möbius committed
107 108 109 110

    action = primitivesMenu->addAction("Pyramid"                    ,this,SLOT(addPyramid()));
    icon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"primitive_pyramid.png");
    action->setIcon(*icon);
111
    whatsThisGen.setWhatsThis(action,tr("Create a Pyramid."),"Pyramid");
Jan Möbius's avatar
Jan Möbius committed
112

113 114
    action = primitivesMenu->addAction("Sphere",this,SLOT(addSphere()));
    icon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"primitive_sphere.png");
Jan Möbius's avatar
Jan Möbius committed
115
    action->setIcon(*icon);
116
    whatsThisGen.setWhatsThis(action,tr("Create a Sphere. "),"Sphere");
Jan Möbius's avatar
Jan Möbius committed
117

118 119
    action = primitivesMenu->addAction("Tetrahedron",this,SLOT(addTetrahedron()));
    icon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"primitive_tetrahedron.png");
Jan Möbius's avatar
Jan Möbius committed
120
    action->setIcon(*icon);
121
    whatsThisGen.setWhatsThis(action,tr("Create a Tetrahedron."),"Tetrahedron");
Jan Möbius's avatar
Jan Möbius committed
122
  }
Jan Möbius's avatar
Jan Möbius committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

}



int PrimitivesGeneratorPlugin::addTriMesh() {
  int objectId = -1;

  emit addEmptyObject( DATA_TRIANGLE_MESH, objectId );

  TriMeshObject* object;
  if ( !PluginFunctions::getObject(objectId,object) ) {
    emit log(LOGERR,"Unable to create new Object");
    return -1;
  }

  return objectId;
}

Jan Möbius's avatar
Jan Möbius committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155
int PrimitivesGeneratorPlugin::addPolyMesh() {
  int objectId = -1;

  emit addEmptyObject( DATA_POLY_MESH, objectId );

  PolyMeshObject* object;
  if ( !PluginFunctions::getObject(objectId,object) ) {
    emit log(LOGERR,"Unable to create new Object");
    return -1;
  }

  return objectId;
}

Jan Möbius's avatar
Jan Möbius committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
int PrimitivesGeneratorPlugin::addTetrahedron() {

  int newObject = addTriMesh();

  TriMeshObject* object;
  if ( !PluginFunctions::getObject(newObject,object) ) {
        emit log(LOGERR,"Unable to create new Object");
        return -1;
  } else {

    object->setName( "Tetrahedron " + QString::number(newObject) );

    triMesh_ =  object->mesh();

    triMesh_->clear();

    // Add 4 vertices
    vhandles_.resize(4);

Jan Möbius's avatar
Jan Möbius committed
175 176 177 178 179 180
    static const double size = 1.0;

    vhandles_[0] = triMesh_->add_vertex(TriMesh::Point(-size, -size,  size));
    vhandles_[1] = triMesh_->add_vertex(TriMesh::Point( size,  size,  size));
    vhandles_[2] = triMesh_->add_vertex(TriMesh::Point(-size,  size, -size));
    vhandles_[3] = triMesh_->add_vertex(TriMesh::Point( size, -size, -size));
Jan Möbius's avatar
Jan Möbius committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

    // Add 4 faces
    add_face(0,1,2);
    add_face(0,2,3);
    add_face(2,1,3);
    add_face(3,1,0);

    triMesh_->update_normals();

    emit updatedObject(newObject,UPDATE_ALL);

    return newObject;
  }

  return -1;
}

int PrimitivesGeneratorPlugin::addTriangulatedCube() {


  int newObject = addTriMesh();

  TriMeshObject* object;
  if ( !PluginFunctions::getObject(newObject,object) ) {
        emit log(LOGERR,"Unable to create new Object");
        return -1;
  } else {

    object->setName( "Cube " + QString::number(newObject) );

    triMesh_ =  object->mesh();

    triMesh_->clear();

    // Add 8 vertices
    vhandles_.resize(8);

Jan Möbius's avatar
Jan Möbius committed
218 219 220 221 222 223 224 225 226 227
    static const double size = 1.0;

    vhandles_[0] = triMesh_->add_vertex(TriMesh::Point( size, -size, size));
    vhandles_[1] = triMesh_->add_vertex(TriMesh::Point( size,  size, size));
    vhandles_[2] = triMesh_->add_vertex(TriMesh::Point(-size,  size, size));
    vhandles_[3] = triMesh_->add_vertex(TriMesh::Point(-size, -size, size));
    vhandles_[4] = triMesh_->add_vertex(TriMesh::Point( size, -size,-size));
    vhandles_[5] = triMesh_->add_vertex(TriMesh::Point( size,  size,-size));
    vhandles_[6] = triMesh_->add_vertex(TriMesh::Point(-size,  size,-size));
    vhandles_[7] = triMesh_->add_vertex(TriMesh::Point(-size, -size,-size));
Jan Möbius's avatar
Jan Möbius committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254


    // Add 12 faces
    add_face(0,1,2);
    add_face(0,2,3);
    add_face(0,5,1);
    add_face(5,0,4);
    add_face(4,0,7);
    add_face(7,0,3);

    add_face(7,3,6);
    add_face(6,3,2);
    add_face(6,2,5);
    add_face(5,2,1);
    add_face(6,5,4);
    add_face(6,4,7);

    triMesh_->update_normals();

    emit updatedObject(newObject,UPDATE_ALL);

    return newObject;
  }

  return -1;
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
//========================================================================
// Sphere
//========================================================================


ACG::Vec3d PrimitivesGeneratorPlugin::positionOnSphere(int _sliceNumber, int _stackNumber)
{
  ACG::Vec3d position;

  double alpha = (M_PI / double(stacks_)) * double(_stackNumber);
  double beta = ((2.0 * M_PI) / double(slices_)) * double(_sliceNumber);

  double ringRadius = sin(alpha);
  position[0] = sin(beta) * ringRadius;
  position[1] = cos(beta) * ringRadius;
  position[2] = cos(alpha);

  return position;
}

//------------------------------------------------------------------------

ACG::Vec2f PrimitivesGeneratorPlugin::texCoordOnSphere(int _sliceNumber, int _stackNumber)
{
  ACG::Vec2f texCoord;

  double alpha = (M_PI / double(stacks_)) * double(_stackNumber);
  texCoord[0] = double(_sliceNumber) / double(slices_);
  texCoord[1] = 0.5 * (cos(alpha) + 1.0);

  return texCoord;
}

//------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
290 291
int PrimitivesGeneratorPlugin::addSphere() {

292 293 294 295 296 297 298 299
  int newObject = addTriMesh();

  TriMeshObject* object;
  if (!PluginFunctions::getObject(newObject, object)) {
    emit log(LOGERR, "Unable to create new Object");
    return -1;
  } else {

Jan Möbius's avatar
Jan Möbius committed
300 301
    object->setName( "Sphere " + QString::number(newObject) );

302
    triMesh_ = object->mesh();
Jan Möbius's avatar
Jan Möbius committed
303 304 305

    triMesh_->clear();

306 307 308 309 310 311 312
    triMesh_->request_vertex_texcoords2D();

    TriMesh::VertexHandle vh;

    vh = triMesh_->add_vertex(positionOnSphere(0, 0));
    triMesh_->set_texcoord2D(vh, texCoordOnSphere(0, 0));

313
    for (int st = 1; st < stacks_; ++st) {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
      for (int sl = 0; sl < slices_; ++sl) {
        vh = triMesh_->add_vertex(positionOnSphere(sl, st));
        triMesh_->set_texcoord2D(vh, texCoordOnSphere(sl, st));
      }
    }

    vh = triMesh_->add_vertex(positionOnSphere(slices_, stacks_));
    triMesh_->set_texcoord2D(vh, texCoordOnSphere(slices_, stacks_));

    std::vector<TriMesh::VertexHandle> vhandles;

    // Add top triangle fan ( Vertex index is shifted by one for the first slice )
    for (int sl = 1; sl < slices_ + 1; ++sl) {

      vhandles.clear();

      vhandles.push_back(triMesh_->vertex_handle(sl));
      vhandles.push_back(triMesh_->vertex_handle(0));
      vhandles.push_back(triMesh_->vertex_handle(1 * 1 + (sl % slices_)));

      triMesh_->add_face(vhandles);
    }

337
    for (int st = 0; st < stacks_ - 2; ++st) {
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

      // Move around one slice
      for (int sl = 0; sl < slices_; ++sl) {

        // Offset 1 because of singular vertex
        unsigned int startTop = 1 + slices_ * st;
        unsigned int startBottom = 1 + slices_ * (st + 1);

        vhandles.clear();

        vhandles.push_back(triMesh_->vertex_handle(startTop + sl));
        vhandles.push_back(triMesh_->vertex_handle(startTop + ((sl + 1) % slices_)));
        vhandles.push_back(triMesh_->vertex_handle(startBottom + sl));

        triMesh_->add_face(vhandles);

        vhandles.clear();

        vhandles.push_back(triMesh_->vertex_handle(startBottom + sl));
        vhandles.push_back(triMesh_->vertex_handle(startTop + ((sl + 1) % slices_)));
        vhandles.push_back(triMesh_->vertex_handle(startBottom + ((sl + 1) % slices_)));

        triMesh_->add_face(vhandles);
      }

    }

365 366
    const int startTop     = 1 + (stacks_ - 2) * slices_;
    const int bottomVertex = 1 + (stacks_ - 1) * slices_;
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

    // Add bottom triangle fan
    for (int sl = 0; sl < slices_; ++sl) {

      vhandles.clear();

      vhandles.push_back(triMesh_->vertex_handle(bottomVertex));
      vhandles.push_back(triMesh_->vertex_handle(startTop + sl));
      vhandles.push_back(triMesh_->vertex_handle(startTop + ((sl + 1) % slices_)));

      triMesh_->add_face(vhandles);
    }

    triMesh_->update_normals();

    emit updatedObject(newObject,UPDATE_ALL);

    return object->id();
  }


Jan Möbius's avatar
Jan Möbius committed
388 389
}

390 391 392 393 394

//========================================================================
// Pyramid
//========================================================================

Jan Möbius's avatar
Jan Möbius committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
int PrimitivesGeneratorPlugin::addPyramid() {
  int newObject = addTriMesh();

  TriMeshObject* object;
  if ( !PluginFunctions::getObject(newObject,object) ) {
        emit log(LOGERR,"Unable to create new Object");
        return -1;
  } else {

    object->setName( "Pyramid " + QString::number(newObject) );

    triMesh_ =  object->mesh();

    triMesh_->clear();

    // Add 5 vertices
    vhandles_.resize(5);

Jan Möbius's avatar
Jan Möbius committed
413 414 415 416 417 418 419 420
    static const double size   = 1.0;

    vhandles_[0] = triMesh_->add_vertex(TriMesh::Point( size, -size, 0.0));
    vhandles_[1] = triMesh_->add_vertex(TriMesh::Point( size,  size, 0.0));
    vhandles_[2] = triMesh_->add_vertex(TriMesh::Point(-size,  size, 0.0));
    vhandles_[3] = triMesh_->add_vertex(TriMesh::Point(-size, -size, 0.0));

    vhandles_[4] = triMesh_->add_vertex(TriMesh::Point(0.0, 0.0, sqrt(2.0)*size));
Jan Möbius's avatar
Jan Möbius committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

    // Add 6 faces
    add_face(2,1,0);
    add_face(3,2,0);
    add_face(4,0,1);

    add_face(3,0,4);
    add_face(4,2,3);
    add_face(1,2,4);

    triMesh_->update_normals();

    emit updatedObject(newObject,UPDATE_ALL);

    return newObject;
  }

  return -1;
}

void PrimitivesGeneratorPlugin::add_face( int _vh1 , int _vh2, int _vh3 ) {
 std::vector<TriMesh::VertexHandle> vhandles;

 vhandles.push_back(vhandles_[_vh1]);
 vhandles.push_back(vhandles_[_vh2]);
 vhandles.push_back(vhandles_[_vh3]);

 triMesh_->add_face(vhandles);

}

Jan Möbius's avatar
Jan Möbius committed
452 453 454 455 456 457 458 459 460 461 462 463
void PrimitivesGeneratorPlugin::add_face( int _vh1 , int _vh2, int _vh3, int _vh4 , int _vh5 ) {
  std::vector<PolyMesh::VertexHandle> vhandles;

  vhandles.push_back(vphandles_[_vh1]);
  vhandles.push_back(vphandles_[_vh2]);
  vhandles.push_back(vphandles_[_vh3]);
  vhandles.push_back(vphandles_[_vh4]);
  vhandles.push_back(vphandles_[_vh5]);

  polyMesh_->add_face(vhandles);
}

Jan Möbius's avatar
Jan Möbius committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
int PrimitivesGeneratorPlugin::addIcosahedron() {
  int newObject = addTriMesh();

  TriMeshObject* object;
  if ( !PluginFunctions::getObject(newObject,object) ) {
        emit log(LOGERR,"Unable to create new Object");
        return -1;
  } else {

    object->setName( "Icosahedron " + QString::number(newObject) );

    triMesh_ =  object->mesh();

    triMesh_->clear();

    // Add 12 vertices
    vhandles_.resize(12);

Jan Möbius's avatar
Jan Möbius committed
482
    double phi  = 0.5 * (1.0 + sqrt(5.0));
Jan Möbius's avatar
Jan Möbius committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    //double norm = 1.0 / sqrt(1.0 + phi*phi);
    double norm = 1.0;

    vhandles_[0 ] = triMesh_->add_vertex(norm * TriMesh::Point(  0.0 ,  -1.0 , -phi ));
    vhandles_[1 ] = triMesh_->add_vertex(norm * TriMesh::Point(  0.0 ,   1.0 , -phi ));
    vhandles_[2 ] = triMesh_->add_vertex(norm * TriMesh::Point(  0.0 ,   1.0 ,  phi ));
    vhandles_[3 ] = triMesh_->add_vertex(norm * TriMesh::Point(  0.0 ,  -1.0 ,  phi ));

    vhandles_[4 ] = triMesh_->add_vertex(norm * TriMesh::Point( -1.0 , -phi ,  0.0  ));
    vhandles_[5 ] = triMesh_->add_vertex(norm * TriMesh::Point(  1.0 , -phi ,  0.0  ));
    vhandles_[6 ] = triMesh_->add_vertex(norm * TriMesh::Point(  1.0 ,  phi ,  0.0  ));
    vhandles_[7 ] = triMesh_->add_vertex(norm * TriMesh::Point( -1.0 ,  phi ,  0.0  ));

    vhandles_[8 ] = triMesh_->add_vertex(norm * TriMesh::Point( -phi ,  0.0 , -1.0  ));
    vhandles_[9 ] = triMesh_->add_vertex(norm * TriMesh::Point( -phi ,  0.0 ,  1.0  ));
    vhandles_[10] = triMesh_->add_vertex(norm * TriMesh::Point(  phi ,  0.0 ,  1.0  ));
    vhandles_[11] = triMesh_->add_vertex(norm * TriMesh::Point(  phi ,  0.0 , -1.0  ));


    // Add 20 faces
    add_face(2,6,7);
    add_face(7,6,1);

    add_face(11,0,1);
    add_face(0, 8,1);

    add_face(4,9,8);
    add_face(8,9,7);

    add_face(9,3,2);
    add_face(10,2,3);

    add_face(5,11,10);
    add_face(11,6,10);

    add_face(0,5,4);
    add_face(5,3,4);

    // Upper block
    add_face(6,2,10);
    add_face(6,11,1);
    add_face(1,8,7);
    add_face(9,2,7);

    // Lower block
    add_face(3,5,10);
    add_face(0,11,5);
    add_face(3,9,4);
    add_face(0,4,8);

    triMesh_->update_normals();

    emit updatedObject(newObject,UPDATE_ALL);

    return newObject;
  }

  return -1;
}

Jan Möbius's avatar
Jan Möbius committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
int PrimitivesGeneratorPlugin::addOctahedron() {
  int newObject = addTriMesh();

  TriMeshObject* object;
  if ( !PluginFunctions::getObject(newObject,object) ) {
    emit log(LOGERR,"Unable to create new Object");
    return -1;
  } else {

    object->setName( "Octahedron " + QString::number(newObject) );

    triMesh_ =  object->mesh();

    triMesh_->clear();

Jan Möbius's avatar
Jan Möbius committed
558
    // Add 6 vertices
Jan Möbius's avatar
Jan Möbius committed
559 560
    vhandles_.resize(6);

Jan Möbius's avatar
Jan Möbius committed
561
    static const double size = sqrt(2.0);
Jan Möbius's avatar
Jan Möbius committed
562

Jan Möbius's avatar
Jan Möbius committed
563 564 565 566 567 568 569
    vhandles_[0 ] = triMesh_->add_vertex(TriMesh::Point(-size,   0.0,   0.0));
    vhandles_[1 ] = triMesh_->add_vertex(TriMesh::Point(  0.0, -size,   0.0));
    vhandles_[2 ] = triMesh_->add_vertex(TriMesh::Point( size,   0.0,   0.0));
    vhandles_[3 ] = triMesh_->add_vertex(TriMesh::Point(  0.0,  size,   0.0));

    vhandles_[4 ] = triMesh_->add_vertex(TriMesh::Point(  0.0,   0.0,  size));
    vhandles_[5 ] = triMesh_->add_vertex(TriMesh::Point(  0.0,   0.0, -size));
Jan Möbius's avatar
Jan Möbius committed
570 571 572 573 574 575 576 577


    // Add 8 faces
    add_face(0,1,4);
    add_face(1,2,4);

    add_face(2,3,4);
    add_face(0,4,3);
Jan Möbius's avatar
Jan Möbius committed
578

Jan Möbius's avatar
Jan Möbius committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    add_face(5,1,0);
    add_face(5,2,1);

    add_face(5,3,2);
    add_face(5,0,3);

    triMesh_->update_normals();

    emit updatedObject(newObject,UPDATE_ALL);

    return newObject;
  }

  return -1;
}

int PrimitivesGeneratorPlugin::addDodecahedron() {
  int newObject = addPolyMesh();

  PolyMeshObject* object;
  if ( !PluginFunctions::getObject(newObject,object) ) {
    emit log(LOGERR,"Unable to create new Object");
    return -1;
  } else {

Jan Möbius's avatar
Jan Möbius committed
604
    object->setName( "Dodecahedron " + QString::number(newObject) );
Jan Möbius's avatar
Jan Möbius committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

    polyMesh_ =  object->mesh();

    polyMesh_->clear();

    // Add 20 vertices
    vphandles_.resize(20);

    double phi = (1.0 + sqrt(5.0)) / 2.0;

    vphandles_[0 ] = polyMesh_->add_vertex(TriMesh::Point(  1.0 ,  1.0 , 1.0 ));
    vphandles_[1 ] = polyMesh_->add_vertex(TriMesh::Point(  1.0 ,  1.0 ,-1.0 ));
    vphandles_[2 ] = polyMesh_->add_vertex(TriMesh::Point(  1.0 , -1.0 , 1.0 ));
    vphandles_[3 ] = polyMesh_->add_vertex(TriMesh::Point(  1.0 , -1.0 ,-1.0 ));
    vphandles_[4 ] = polyMesh_->add_vertex(TriMesh::Point( -1.0 ,  1.0 , 1.0 ));
    vphandles_[5 ] = polyMesh_->add_vertex(TriMesh::Point( -1.0 ,  1.0 ,-1.0 ));
    vphandles_[6 ] = polyMesh_->add_vertex(TriMesh::Point( -1.0 , -1.0 , 1.0 ));
    vphandles_[7 ] = polyMesh_->add_vertex(TriMesh::Point( -1.0 , -1.0 ,-1.0 ));

    vphandles_[8 ] = polyMesh_->add_vertex(TriMesh::Point( 0.0 ,  1.0 / phi ,  phi ));
    vphandles_[9 ] = polyMesh_->add_vertex(TriMesh::Point( 0.0 ,  1.0 / phi , -phi ));
    vphandles_[10] = polyMesh_->add_vertex(TriMesh::Point( 0.0 , -1.0 / phi ,  phi ));
    vphandles_[11] = polyMesh_->add_vertex(TriMesh::Point( 0.0 , -1.0 / phi , -phi ));

    vphandles_[12] = polyMesh_->add_vertex(TriMesh::Point(  1.0 / phi ,  phi, 0.0));
    vphandles_[13] = polyMesh_->add_vertex(TriMesh::Point(  1.0 / phi , -phi, 0.0));
    vphandles_[14] = polyMesh_->add_vertex(TriMesh::Point( -1.0 / phi ,  phi, 0.0));
    vphandles_[15] = polyMesh_->add_vertex(TriMesh::Point( -1.0 / phi , -phi, 0.0));

    vphandles_[16] = polyMesh_->add_vertex(TriMesh::Point(  phi , 0.0 , 1.0 / phi));
    vphandles_[17] = polyMesh_->add_vertex(TriMesh::Point(  phi , 0.0 ,-1.0 / phi));
    vphandles_[18] = polyMesh_->add_vertex(TriMesh::Point( -phi , 0.0 , 1.0 / phi));
    vphandles_[19] = polyMesh_->add_vertex(TriMesh::Point( -phi , 0.0 ,-1.0 / phi));

    // Add 12 faces
    add_face(14, 5,19,18, 4);
    add_face( 5, 9,11, 7,19);
    add_face( 6,15,13, 2,10);
    add_face(12, 0,16,17, 1);

    add_face( 0, 8,10, 2,16);
    add_face(16, 2,13, 3,17);
    add_face( 3,13,15, 7,11);
    add_face( 7,15, 6,18,19);

    add_face( 4,18, 6,10, 8);
    add_face( 4, 8, 0,12,14);
    add_face(14,12, 1, 9, 5);
    add_face( 9, 1,17, 3,11);

    polyMesh_->update_normals();

    emit updatedObject(newObject,UPDATE_ALL);

    return newObject;
  }

  return -1;
}
Jan Möbius's avatar
Jan Möbius committed
664 665 666 667 668 669




Q_EXPORT_PLUGIN2( primitivesgeneratorplugin , PrimitivesGeneratorPlugin );