@@ -458,10 +458,40 @@ boost::optional<ToolPaths> PlaneSlicerRasterGenerator::generate()
458458 {
459459 CONSOLE_BRIDGE_logDebug (" %s No mesh data has been provided" ,getName ().c_str ());
460460 }
461- // computing mayor axis using oob
462- vtkSmartPointer<vtkOBBTree> oob = vtkSmartPointer<vtkOBBTree>::New ();
463- Vector3d corner, x_dir, y_dir, z_dir, sizes;// x is longest, y is mid and z is smallest
464- oob->ComputeOBB (mesh_data_,corner.data (), x_dir.data (), y_dir.data (), z_dir.data (), sizes.data ());
461+ // Assign the longest axis of the bounding box to x, middle to y, and shortest to z.
462+ Vector3d corner, x_dir, y_dir, z_dir, sizes;
463+ if (config_.raster_wrt_global_axes )
464+ {
465+ // Determine extent of mesh along axes of current coordinate frame
466+ VectorXd bounds (6 );
467+ std::vector<Vector3d> extents;
468+ mesh_data_->GetBounds (bounds.data ());
469+ extents.push_back (Vector3d::UnitX () * (bounds[1 ] - bounds[0 ])); // Extent in x-direction of supplied mesh coordinate frame
470+ extents.push_back (Vector3d::UnitY () * (bounds[3 ] - bounds[2 ])); // Extent in y-direction of supplied mesh coordinate frame
471+ extents.push_back (Vector3d::UnitZ () * (bounds[5 ] - bounds[4 ])); // Extent in z-direction of supplied mesh coordinate frame
472+
473+ // find min and max magnitude.
474+ int max = 0 ;
475+ int min = 0 ;
476+ for (std::size_t i = 1 ; i < extents.size (); ++i)
477+ {
478+ if (extents[max].squaredNorm () < extents[i].squaredNorm ())
479+ max = i;
480+ else if (extents[min].squaredNorm () > extents[i].squaredNorm ())
481+ min = i;
482+ }
483+
484+ // Assign the axes in order. Computing y saves comparisons and guarantees right-handedness.
485+ x_dir = extents[max].normalized ();
486+ z_dir = extents[min].normalized ();
487+ y_dir = z_dir.cross (x_dir).normalized ();
488+ }
489+ else
490+ {
491+ // computing major axes using oob and assign to x_dir, y_dir, z_dir
492+ vtkSmartPointer<vtkOBBTree> oob = vtkSmartPointer<vtkOBBTree>::New ();
493+ oob->ComputeOBB (mesh_data_,corner.data (), x_dir.data (), y_dir.data (), z_dir.data (), sizes.data ());
494+ }
465495
466496 // Compute the center of mass
467497 Vector3d origin;
@@ -498,10 +528,11 @@ boost::optional<ToolPaths> PlaneSlicerRasterGenerator::generate()
498528 half_ext = sizes / 2.0 ;
499529 center = Eigen::Vector3d (bounds[0 ], bounds[2 ], bounds[3 ]) + half_ext;
500530
501- // now cutting the mesh with planes along the y axis
502- // @todo This should be calculated instead of being fixed along the y-axis
531+ // Apply the rotation offset about the short direction (new Z axis) of the bounding box
503532 Isometry3d rotation_offset = Isometry3d::Identity () * AngleAxisd (config_.raster_rot_offset , Vector3d::UnitZ ());
504- Vector3d raster_dir = (rotation_offset * Vector3d::UnitY ()).normalized ();
533+
534+ // Calculate direction of raster strokes, rotated by the above-specified amount
535+ Vector3d raster_dir = (rotation_offset * config_.raster_direction ).normalized ();
505536
506537 // Calculate all 8 corners projected onto the raster direction vector
507538 Eigen::VectorXd dist (8 );
0 commit comments