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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 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 218 219 220 221 222 223 224 225 226 227 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 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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 365
|
#include "Scene_plane_item.h"
#include <CGAL/Three/Triangle_container.h>
#include <CGAL/Three/Edge_container.h>
#include <QApplication>
#include <CGAL/Three/Three.h>
#include <QInputDialog>
#include <QMessageBox>
#include <QRegularExpression>
#include <QMenu>
using namespace CGAL::Three;
typedef Triangle_container Tc;
typedef Edge_container Ec;
typedef Viewer_interface VI;
Scene_plane_item::Scene_plane_item(const CGAL::Three::Scene_interface* scene_interface)
:scene(scene_interface),
manipulable(false),
can_clone(true),
frame(new ManipulatedFrame())
{
setNormal(0., 0., 1.);
setTriangleContainer(0, new Tc(VI::PROGRAM_NO_SELECTION,
false));
setEdgeContainer(0, new Ec(VI::PROGRAM_NO_SELECTION,
false));
//Generates an integer which will be used as ID for each buffer
invalidateOpenGLBuffers();
}
Scene_plane_item::~Scene_plane_item() {
delete frame;
}
void Scene_plane_item::initializeBuffers(Viewer_interface *viewer) const
{
getTriangleContainer(0)->initializeBuffers(viewer);
getTriangleContainer(0)->setFlatDataSize(nb_quads);
positions_quad.resize(0);
positions_quad.shrink_to_fit();
getEdgeContainer(0)->initializeBuffers(viewer);
getEdgeContainer(0)->setFlatDataSize(nb_lines);
positions_lines.resize(0);
positions_lines.shrink_to_fit();
}
void Scene_plane_item::computeElements() const
{
QApplication::setOverrideCursor(Qt::WaitCursor);
positions_quad.resize(0);
positions_lines.resize(0);
const double diag = scene_diag();
//The quad
{
positions_quad.push_back(-diag);
positions_quad.push_back(-diag);
positions_quad.push_back(0.0 );
positions_quad.push_back(-diag);
positions_quad.push_back(diag );
positions_quad.push_back(0.0 );
positions_quad.push_back(diag );
positions_quad.push_back(-diag);
positions_quad.push_back(0.0 );
positions_quad.push_back(diag );
positions_quad.push_back(-diag);
positions_quad.push_back(0.0 );
positions_quad.push_back(-diag);
positions_quad.push_back(diag );
positions_quad.push_back(0.0 );
positions_quad.push_back(diag );
positions_quad.push_back(diag );
positions_quad.push_back(0.0 );
getTriangleContainer(0)->allocate(Tc::Flat_vertices, positions_quad.data(),
static_cast<int>(positions_quad.size()
* sizeof(float)));
}
//The grid
float x = (2*diag)/10.0;
float y = (2*diag)/10.0;
{
for(float u = 0; u < 11; u += 1.f)
{
positions_lines.push_back(-diag + x* u);
positions_lines.push_back(-diag );
positions_lines.push_back(0.0 );
positions_lines.push_back(-diag + x* u);
positions_lines.push_back(diag );
positions_lines.push_back(0.0 );
}
for(float v=0; v<11; v += 1.f)
{
positions_lines.push_back(-diag );
positions_lines.push_back(-diag + v * y);
positions_lines.push_back(0.0 );
positions_lines.push_back(diag );
positions_lines.push_back(-diag + v * y);
positions_lines.push_back(0.0 );
}
getEdgeContainer(0)->allocate(Ec::Vertices, positions_lines.data(),
static_cast<int>(positions_lines.size()
* sizeof(float)));
}
nb_quads = positions_quad.size();
nb_lines = positions_lines.size();
setBuffersFilled(true);
QApplication::restoreOverrideCursor();
}
void Scene_plane_item::draw(Viewer_interface* viewer)const
{
if(!isInit(viewer))
initGL(viewer);
if ( getBuffersFilled() &&
! getBuffersInit(viewer))
{
initializeBuffers(viewer);
setBuffersInit(viewer, true);
}
if(!getBuffersFilled())
{
computeElements();
initializeBuffers(viewer);
}
QMatrix4x4 f_matrix;
for(int i=0; i<16; i++)
f_matrix.data()[i] = (float)frame->matrix()[i];
getTriangleContainer(0)->setFrameMatrix(f_matrix);
getTriangleContainer(0)->setColor(this->color());
getTriangleContainer(0)->draw(viewer, true);
}
void Scene_plane_item::drawEdges(CGAL::Three::Viewer_interface* viewer)const
{
if(!isInit(viewer))
initGL(viewer);
if ( getBuffersFilled() &&
! getBuffersInit(viewer))
{
initializeBuffers(viewer);
setBuffersInit(viewer, true);
}
if(!getBuffersFilled())
{
computeElements();
initializeBuffers(viewer);
}
QMatrix4x4 f_matrix;
for(int i=0; i<16; i++)
f_matrix.data()[i] = (float)frame->matrix()[i];
getEdgeContainer(0)->setFrameMatrix(f_matrix);
getEdgeContainer(0)->setColor(QColor(0,0,0));
getEdgeContainer(0)->draw(viewer, true);
}
void Scene_plane_item::flipPlane()
{
CGAL::qglviewer::Quaternion q;
CGAL::qglviewer::Vec axis(0,1,0);
if(frame->orientation().axis() == axis)
q.setAxisAngle(CGAL::qglviewer::Vec(1,0,0), CGAL_PI);
else
q.setAxisAngle(axis, CGAL_PI);
frame->rotate(q.normalized());
invalidateOpenGLBuffers();
Q_EMIT itemChanged();
}
bool Scene_plane_item::manipulatable() const {
return manipulable;
}
Scene_item::ManipulatedFrame* Scene_plane_item::manipulatedFrame() {
return frame;
}
Scene_plane_item* Scene_plane_item::clone() const {
if(can_clone)
{
Scene_plane_item* item = new Scene_plane_item(scene);
item->manipulable = manipulable;
item->can_clone = true;
item->frame = new ManipulatedFrame;
item->frame->setPosition(frame->position());
item->frame->setOrientation(frame->orientation());
return item;
}
else
return nullptr;
}
QString Scene_plane_item::toolTip() const {
const CGAL::qglviewer::Vec offset = static_cast<CGAL::Three::Viewer_interface*>(CGAL::QGLViewer::QGLViewerPool().first())->offset();
const CGAL::qglviewer::Vec& pos = frame->position() - offset;
const CGAL::qglviewer::Vec& n = frame->inverseTransformOf(CGAL::qglviewer::Vec(0.f, 0.f, 1.f));
return
tr("<p><b>%1</b> (mode: %2, color: %3)<br />")
.arg(this->name())
.arg(this->renderingModeName())
.arg(this->color().name())
+
tr("<i>Plane</i></p>"
"<p>Equation: %1*x + %2*y + %3*z + %4 = 0<br />"
"Normal vector: (%1, %2, %3)<br />"
"Point: (%5, %6, %7)</p>")
.arg(n[0]).arg(n[1]).arg(n[2])
.arg( - pos * n)
.arg(pos[0]).arg(pos[1]).arg(pos[2])
+
tr("<p>Can clone: %1<br />"
"Manipulatable: %2</p>")
.arg(can_clone?tr("true"):tr("false"))
.arg(manipulable?tr("true"):tr("false"));
}
Plane_3 Scene_plane_item::plane(CGAL::qglviewer::Vec offset) const {
const CGAL::qglviewer::Vec& pos = frame->position() - offset;
const CGAL::qglviewer::Vec& n =
frame->inverseTransformOf(CGAL::qglviewer::Vec(0.f, 0.f, 1.f));
return Plane_3(n[0], n[1], n[2], - n * pos);
}
void Scene_plane_item::invalidateOpenGLBuffers()
{
setBuffersFilled(false);
getTriangleContainer(0)->reset_vbos(ALL);
getEdgeContainer(0)->reset_vbos(ALL);
}
void Scene_plane_item::setPosition(float x, float y, float z) {
frame->setPosition(x, y, z);
}
void Scene_plane_item::setPosition(double x, double y, double z) {
const CGAL::qglviewer::Vec offset = static_cast<CGAL::Three::Viewer_interface*>(CGAL::QGLViewer::QGLViewerPool().first())->offset();
frame->setPosition((float)x+offset.x, (float)y+offset.y, (float)z+offset.z);
}
void Scene_plane_item::setNormal(float x, float y, float z) {
QVector3D normal(x,y,z);
if(normal == QVector3D(0,0,0))
return;
QVector3D origin(0,0,1);
CGAL::qglviewer::Quaternion q;
if(origin == normal)
{
return;
}
if(origin == -normal)
{
q.setAxisAngle(CGAL::qglviewer::Vec(0,1,0),CGAL_PI);
frame->setOrientation(q);
return;
}
QVector3D cp = QVector3D::crossProduct(origin, normal);
cp.normalize();
q.setAxisAngle(CGAL::qglviewer::Vec(cp.x(),cp.y(), cp.z()),acos(QVector3D::dotProduct(origin, normal)/(normal.length()*origin.length())));
frame->setOrientation(q.normalized());
}
void Scene_plane_item::setNormal(double x, double y, double z) {
setNormal((float)x, (float)y, (float)z);
}
void Scene_plane_item::setClonable(bool b) {
can_clone = b;
}
void Scene_plane_item::setManipulatable(bool b) {
manipulable = b;
}
QMenu* Scene_plane_item::contextMenu()
{
QMenu* menu = Scene_item::contextMenu();
const char* prop_name = "Menu modified by Scene_plane_item.";
bool menuChanged = menu->property(prop_name).toBool();
if(!menuChanged) {
menu->addSeparator();
QAction* actionOrientPlane=
menu->addAction(tr("Set Plane Equation"));
actionOrientPlane->setObjectName("actionOrientPlane");
connect(actionOrientPlane, &QAction::triggered,
this, &Scene_plane_item::setPlaneOrientation);
setProperty("menu_changed", true);
menu->setProperty(prop_name, true);
}
return menu;
}
void Scene_plane_item::setPlaneOrientation()
{
bool does_match = true;
//check that the result is of the form %1*x + %2*y + %3*z + %4 = 0, modulo the whitespaces.
QRegularExpression rx(
"(\\-?\\s*\\d*\\.?\\d*(?:e\\-?\\d*)?)\\s*\\*\\s*x\\s*\\+?\\s*(\\-?\\s*\\d*\\.?\\d*(?:e\\-?\\d*)?)\\s*\\*\\s*y\\s*\\+?\\s*(\\-?\\s*\\d*\\.?\\d*(?:e\\-?\\d*)?)\\s*\\*\\s*z\\s*\\+?\\s*(\\-?\\s*\\d*\\.?\\d*(?:e\\-?\\d*)?)\\s*=\\s*0"
);
const CGAL::qglviewer::Vec offset = static_cast<CGAL::Three::Viewer_interface*>(CGAL::QGLViewer::QGLViewerPool().first())->offset();
QVector3D qoffset(offset.x, offset.y, offset.z);
const CGAL::qglviewer::Vec& pos = frame->position();
const CGAL::qglviewer::Vec& n = frame->inverseTransformOf(CGAL::qglviewer::Vec(0.f, 0.f, 1.f));
QRegularExpressionMatch match;
do{
bool ok;
QString placeHolder = tr("%1*x + %2*y + %3*z + %4 = 0")
.arg(n[0]).arg(n[1]).arg(n[2]).arg( - (pos -offset)* n);
QString eq = QInputDialog::getText(CGAL::Three::Three::mainWindow(),
"Set Plane Equation",
"Equation",
QLineEdit::Normal,
placeHolder,
&ok);
if(!ok)
return;
match = rx.match(eq);
does_match = match.hasMatch() && match.capturedLength(0) == eq.size();
if(!does_match)
{
QMessageBox::warning(CGAL::Three::Three::mainWindow(),"Error","The input must be of the form a*x+b*y+c*z+d=0");
}
}while(!does_match);
double a(match.captured(1).toDouble()), b(match.captured(2).toDouble()), c(match.captured(3).toDouble()), d(match.captured(4).toDouble());
Kernel_epic::Point_3 sure_point(0,0,0);
if(c != 0)
sure_point = Kernel_epic::Point_3(offset.x, offset.y, offset.z-d/c);
else if (b !=0)
sure_point = Kernel_epic::Point_3(offset.x, offset.y-d/b, offset.z);
else if (a !=0)
sure_point = Kernel_epic::Point_3(offset.x-d/a, offset.y, offset.z);
Kernel_epic::Plane_3 pl(sure_point, Kernel_epic::Vector_3(a,b,c));
QVector3D normal(a,b,c);
normal.normalize();
setNormal(normal.x(), normal.y(), normal.z());
Kernel_epic::Point_3 bbox_center(
(scene->bbox().xmin() + scene->bbox().xmax()) /2.0 +offset.x,
(scene->bbox().ymin() + scene->bbox().ymax()) /2.0 +offset.y,
(scene->bbox().zmin() + scene->bbox().zmax()) /2.0 +offset.z );
bbox_center = pl.projection(bbox_center);
QVector3D new_pos =
QVector3D(bbox_center.x(), bbox_center.y(), bbox_center.z());
setPosition(new_pos.x(), new_pos.y(), new_pos.z());
invalidateOpenGLBuffers();
itemChanged();
}
|