SbBox3f ViewProviderDatum::getRelevantBoundBox () const { std::vector<App::DocumentObject *> objs; // Probe body first PartDesign::Body* body = PartDesign::Body::findBodyOf ( this->getObject() ); if (body) { objs = body->getFullModel (); } else { // Probe if we belongs to some group App::DocumentObjectGroup* group = App::DocumentObjectGroup::getGroupOfObject ( this->getObject () ); if(group) { objs = group->getObjects (); } else { // Fallback to whole document objs = this->getObject ()->getDocument ()->getObjects (); } } Gui::View3DInventorViewer* viewer = static_cast<Gui::View3DInventor*>(this->getActiveView())->getViewer(); SoGetBoundingBoxAction bboxAction(viewer->getSoRenderManager()->getViewportRegion()); SbBox3f bbox = getRelevantBoundBox (bboxAction, objs); if ( bbox.getVolume () < Precision::Confusion() ) { bbox.extendBy ( defaultBoundBox () ); } return bbox; }
MyPaintable(Gui::View3DInventorViewer* v) :view(v), img(v->getGLWidget()->size(), QImage::Format_ARGB32) { img.fill(qRgba(255, 255, 255, 0)); { QPainter p(&img); p.setPen(Qt::white); p.drawText(200,200,QString::fromLatin1("Render to QImage")); } img = QGLWidget::convertToGLFormat(img); fbo = new QGLFramebufferObject(v->getGLWidget()->size()); fbo->bind(); //glClear(GL_COLOR_BUFFER_BIT); fbo->release(); { QPainter p(fbo); p.setPen(Qt::white); p.drawText(200,200,QString::fromLatin1("Render to QGLFramebufferObject")); p.end(); //img = fbo->toImage(); //img = QGLWidget::convertToGLFormat(img); } //fbo->bind(); //glEnable(GL_DEPTH_TEST); //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glDepthRange(0.1,1.0); //glEnable(GL_LINE_SMOOTH); //SoGLRenderAction a(SbViewportRegion(128,128)); //a.apply(v->getSceneManager()->getSceneGraph()); //fbo->release(); //img = fbo->toImage(); //img = QGLWidget::convertToGLFormat(img); view->getSoRenderManager()->scheduleRedraw(); }
void TaskCreateNodeSet::DefineNodesCallback(void * ud, SoEventCallback * n) { // show the wait cursor because this could take quite some time Gui::WaitCursor wc; TaskCreateNodeSet *taskBox = static_cast<TaskCreateNodeSet *>(ud); // When this callback function is invoked we must in either case leave the edit mode Gui::View3DInventorViewer* view = reinterpret_cast<Gui::View3DInventorViewer*>(n->getUserData()); view->setEditing(false); view->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), DefineNodesCallback,ud); n->setHandled(); SbBool clip_inner; std::vector<SbVec2f> clPoly = view->getGLPolygon(&clip_inner); if (clPoly.size() < 3) return; if (clPoly.front() != clPoly.back()) clPoly.push_back(clPoly.front()); SoCamera* cam = view->getSoRenderManager()->getCamera(); SbViewVolume vv = cam->getViewVolume(); Gui::ViewVolumeProjection proj(vv); Base::Polygon2D polygon; for (std::vector<SbVec2f>::const_iterator it = clPoly.begin(); it != clPoly.end(); ++it) polygon.Add(Base::Vector2D((*it)[0],(*it)[1])); taskBox->DefineNodes(polygon,proj,clip_inner); }
void NaviCubeImplementation::setHilite(int hilite) { if (hilite != m_HiliteId) { m_HiliteId = hilite; //cerr << "m_HiliteFace " << m_HiliteId << endl; m_View3DInventorViewer->getSoRenderManager()->scheduleRedraw(); } }
static void selectionCallback(void * ud, SoEventCallback * cb) { Gui::View3DInventorViewer* view = reinterpret_cast<Gui::View3DInventorViewer*>(cb->getUserData()); view->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), selectionCallback, ud); SoNode* root = view->getSceneGraph(); static_cast<Gui::SoFCUnifiedSelection*>(root)->selectionRole.setValue(true); std::vector<SbVec2f> picked = view->getGLPolygon(); SoCamera* cam = view->getSoRenderManager()->getCamera(); SbViewVolume vv = cam->getViewVolume(); Gui::ViewVolumeProjection proj(vv); Base::Polygon2d polygon; if (picked.size() == 2) { SbVec2f pt1 = picked[0]; SbVec2f pt2 = picked[1]; polygon.Add(Base::Vector2d(pt1[0], pt1[1])); polygon.Add(Base::Vector2d(pt1[0], pt2[1])); polygon.Add(Base::Vector2d(pt2[0], pt2[1])); polygon.Add(Base::Vector2d(pt2[0], pt1[1])); } else { for (std::vector<SbVec2f>::const_iterator it = picked.begin(); it != picked.end(); ++it) polygon.Add(Base::Vector2d((*it)[0],(*it)[1])); } FaceColors* self = reinterpret_cast<FaceColors*>(ud); self->d->view = 0; if (self->d->obj && self->d->obj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) { cb->setHandled(); const TopoDS_Shape& shape = static_cast<Part::Feature*>(self->d->obj)->Shape.getValue(); self->d->addFacesToSelection(view, proj, polygon, shape); view->redraw(); } }
Teapots(Gui::View3DInventorViewer* v) :view(v) { const SbViewportRegion vp = view->getSoRenderManager()->getViewportRegion(); SbVec2s size = vp.getViewportSizePixels(); rubberBandIsShown = false; // makeCurrent(); fbObject = new QGLFramebufferObject(size[0],size[1], QGLFramebufferObject::Depth); //initializeGL(); resizeGL(size[0],size[1]); rubberBandIsShown = true; rubberBandCorner1.setX(200); rubberBandCorner1.setY(200); rubberBandCorner2.setX(800); rubberBandCorner2.setY(600); view->getSoRenderManager()->scheduleRedraw(); }
void Command::adjustCameraPosition() { Gui::Document* doc = Gui::Application::Instance->activeDocument(); if (doc) { Gui::View3DInventor* view = static_cast<Gui::View3DInventor*>(doc->getActiveView()); Gui::View3DInventorViewer* viewer = view->getViewer(); SoCamera* camera = viewer->getSoRenderManager()->getCamera(); if (!camera || !camera->isOfType(SoOrthographicCamera::getClassTypeId())) return; // get scene bounding box SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion()); action.apply(viewer->getSceneGraph()); SbBox3f box = action.getBoundingBox(); if (box.isEmpty()) return; // get cirumscribing sphere and check if camera is inside SbVec3f cam_pos = camera->position.getValue(); SbVec3f box_cnt = box.getCenter(); SbSphere bs; bs.circumscribe(box); float radius = bs.getRadius(); float distance_to_midpoint = (box_cnt-cam_pos).length(); if (radius >= distance_to_midpoint) { // Move the camera to the edge of the bounding sphere, while still // pointing at the scene. SbVec3f direction = cam_pos - box_cnt; (void) direction.normalize(); // we know this is not a null vector camera->position.setValue(box_cnt + direction * radius); // New distance to mid point distance_to_midpoint = (camera->position.getValue() - box.getCenter()).length(); camera->nearDistance = distance_to_midpoint - radius; camera->farDistance = distance_to_midpoint + radius; camera->focalDistance = distance_to_midpoint; } } }
void ViewProviderStructured::cut(const std::vector<SbVec2f>& picked, Gui::View3DInventorViewer &Viewer) { // create the polygon from the picked points Base::Polygon2D cPoly; for (std::vector<SbVec2f>::const_iterator it = picked.begin(); it != picked.end(); ++it) { cPoly.Add(Base::Vector2D((*it)[0],(*it)[1])); } // get a reference to the point feature Points::Feature* fea = static_cast<Points::Feature*>(pcObject); const Points::PointKernel& points = fea->Points.getValue(); SoCamera* pCam = Viewer.getSoRenderManager()->getCamera(); SbViewVolume vol = pCam->getViewVolume(); // search for all points inside/outside the polygon Points::PointKernel newKernel; newKernel.reserve(points.size()); bool invalidatePoints = false; double nan = std::numeric_limits<double>::quiet_NaN(); for (Points::PointKernel::const_iterator jt = points.begin(); jt != points.end(); ++jt) { // valid point? Base::Vector3d vec(*jt); if (!(boost::math::isnan(jt->x) || boost::math::isnan(jt->y) || boost::math::isnan(jt->z))) { SbVec3f pt(jt->x,jt->y,jt->z); // project from 3d to 2d vol.projectToScreen(pt, pt); if (cPoly.Contains(Base::Vector2D(pt[0],pt[1]))) { invalidatePoints = true; vec.Set(nan, nan, nan); } } newKernel.push_back(vec); } if (invalidatePoints) { //Remove the points from the cloud and open a transaction object for the undo/redo stuff Gui::Application::Instance->activeDocument()->openCommand("Cut points"); // sets the points outside the polygon to update the Inventor node fea->Points.setValue(newKernel); // unset the modified flag because we don't need the features' execute() to be called Gui::Application::Instance->activeDocument()->commitCommand(); fea->purgeTouched(); } }
void NaviCubeImplementation::handleResize() { SbVec2s view = m_View3DInventorViewer->getSoRenderManager()->getSize(); if ((m_PrevWidth != view[0]) || (m_PrevHeight != view[1])) { if ((m_PrevWidth > 0) && (m_PrevHeight > 0)) { // maintain position relative to closest edge if (m_CubeWidgetPosX > m_PrevWidth / 2) m_CubeWidgetPosX = view[0] - (m_PrevWidth -m_CubeWidgetPosX); if (m_CubeWidgetPosY > m_PrevHeight / 2) m_CubeWidgetPosY = view[1] - (m_PrevHeight - m_CubeWidgetPosY); } else { // initial position switch (m_Corner) { case NaviCube::TopLeftCorner: m_CubeWidgetPosX = m_CubeWidgetSize*1.1 / 2; m_CubeWidgetPosY = view[1] - m_CubeWidgetSize*1.1 / 2; break; case NaviCube::TopRightCorner: m_CubeWidgetPosX = view[0] - m_CubeWidgetSize*1.1 / 2; m_CubeWidgetPosY = view[1] - m_CubeWidgetSize*1.1 / 2; break; case NaviCube::BottomLeftCorner: m_CubeWidgetPosX = m_CubeWidgetSize*1.1 / 2; m_CubeWidgetPosY = m_CubeWidgetSize*1.1 / 2; break; case NaviCube::BottomRightCorner: m_CubeWidgetPosX = view[0] - m_CubeWidgetSize*1.1 / 2; m_CubeWidgetPosY = m_CubeWidgetSize*1.1 / 2; break; } } m_PrevWidth = view[0]; m_PrevHeight = view[1]; m_View3DInventorViewer->getSoRenderManager()->scheduleRedraw(); } }
void paintGL() { const SbViewportRegion vp = view->getSoRenderManager()->getViewportRegion(); SbVec2s size = vp.getViewportSizePixels(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, size[0], 0, size[1], -1, 1); glPushAttrib(GL_ALL_ATTRIB_BITS); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4d(0.0,0.0,1.0,0.0f); glRasterPos2d(0,0); //http://wiki.delphigl.com/index.php/Multisampling //glDrawPixels(img.width(),img.height(),GL_RGBA,GL_UNSIGNED_BYTE,img.bits()); /* fbo->bind(); GLuint* buf = new GLuint[size[0]*size[1]]; glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE, buf); fbo->release(); glDrawPixels(size[0],size[1],GL_RGBA,GL_UNSIGNED_BYTE,buf); delete [] buf; */ /* glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, fbo->texture()); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex2f(-1.0,-1.0); glTexCoord2f(0.0,1.0); glVertex2f(-1.0,1.0); glTexCoord2f(1.0,1.0); glVertex2f(1.0,1.0); glTexCoord2f(1.0,0.0); glVertex2f(1.0,-1.0); glEnd(); glDisable(GL_TEXTURE_2D); */ glPopAttrib(); glPopMatrix(); }
void paintGL() { const SbViewportRegion vp = view->getSoRenderManager()->getViewportRegion(); SbVec2s size = vp.getViewportSizePixels(); //glDisable(GL_LIGHTING); //glViewport(0, 0, size[0], size[1]); //glMatrixMode(GL_PROJECTION); //glLoadIdentity(); //glMatrixMode(GL_MODELVIEW); //glLoadIdentity(); //glDisable(GL_DEPTH_TEST); //glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glOrtho(0, size[0], size[1], 0, 0, 100); glMatrixMode(GL_MODELVIEW); glDisable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLineWidth(4.0); glColor4f(1.0f, 1.0f, 1.0f, 0.2f); glRecti(rubberBandCorner1.x(), rubberBandCorner1.y(), rubberBandCorner2.x(), rubberBandCorner2.y()); glColor4f(1.0, 1.0, 0.0, 0.5); glLineStipple(3, 0xAAAA); glEnable(GL_LINE_STIPPLE); glBegin(GL_LINE_LOOP); glVertex2i(rubberBandCorner1.x(), rubberBandCorner1.y()); glVertex2i(rubberBandCorner2.x(), rubberBandCorner1.y()); glVertex2i(rubberBandCorner2.x(), rubberBandCorner2.y()); glVertex2i(rubberBandCorner1.x(), rubberBandCorner2.y()); glEnd(); glLineWidth(1.0); glDisable(GL_LINE_STIPPLE); glDisable(GL_BLEND); //glEnable(GL_LIGHTING); //glEnable(GL_DEPTH_TEST); }
void ViewProviderPoints::cut(const std::vector<SbVec2f>& picked, Gui::View3DInventorViewer &Viewer) { // create the polygon from the picked points Base::Polygon2D cPoly; for (std::vector<SbVec2f>::const_iterator it = picked.begin(); it != picked.end(); ++it) { cPoly.Add(Base::Vector2D((*it)[0],(*it)[1])); } // get a reference to the point feature Points::Feature* fea = (Points::Feature*)pcObject; const Points::PointKernel& points = fea->Points.getValue(); SoCamera* pCam = Viewer.getSoRenderManager()->getCamera(); SbViewVolume vol = pCam->getViewVolume(); // search for all points inside/outside the polygon Points::PointKernel newKernel; for ( Points::PointKernel::const_iterator jt = points.begin(); jt != points.end(); ++jt ) { SbVec3f pt(jt->x,jt->y,jt->z); // project from 3d to 2d vol.projectToScreen(pt, pt); if (!cPoly.Contains(Base::Vector2D(pt[0],pt[1]))) newKernel.push_back(*jt); } if (newKernel.size() == points.size()) return; // nothing needs to be done //Remove the points from the cloud and open a transaction object for the undo/redo stuff Gui::Application::Instance->activeDocument()->openCommand("Cut points"); // sets the points outside the polygon to update the Inventor node fea->Points.setValue(newKernel); // unset the modified flag because we don't need the features' execute() to be called Gui::Application::Instance->activeDocument()->commitCommand(); fea->purgeTouched(); }
void resizeGL(int width, int height) { #if 0 fbObject->bind(); glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (width <= height) { glOrtho(0.0, 20.0, 0.0, 20.0 * GLfloat(height) / GLfloat(width), -10.0, 10.0); } else { glOrtho(0.0, 20.0 * GLfloat(width) / GLfloat(height), 0.0, 20.0, -10.0, 10.0); } glMatrixMode(GL_MODELVIEW); drawTeapots(); fbObject->release(); #else (void)width; (void)height; fbObject->bind(); glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); glEnable(GL_LINE_SMOOTH); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDepthRange(0.1,1.0); SoGLRenderAction gl(SbViewportRegion(fbObject->size().width(),fbObject->size().height())); gl.apply(view->getSoRenderManager()->getSceneGraph()); fbObject->release(); #endif }
void ViewProviderBody::updateOriginDatumSize () { PartDesign::Body *body = static_cast<PartDesign::Body *> ( getObject() ); // Use different bounding boxes for datums and for origins: Gui::Document* gdoc = Gui::Application::Instance->getDocument(getObject()->getDocument()); if(!gdoc) return; Gui::MDIView* view = gdoc->getViewOfViewProvider(this); if(!view) return; Gui::View3DInventorViewer* viewer = static_cast<Gui::View3DInventor*>(view)->getViewer(); SoGetBoundingBoxAction bboxAction(viewer->getSoRenderManager()->getViewportRegion()); const auto & model = body->getFullModel (); // BBox for Datums is calculated from all visible objects but treating datums as their basepoints only SbBox3f bboxDatums = ViewProviderDatum::getRelevantBoundBox ( bboxAction, model ); // BBox for origin should take into account datums size also SbBox3f bboxOrigins = bboxDatums; for(App::DocumentObject* obj : model) { if ( obj->isDerivedFrom ( Part::Datum::getClassTypeId () ) ) { ViewProvider *vp = Gui::Application::Instance->getViewProvider(obj); if (!vp) { continue; } ViewProviderDatum *vpDatum = static_cast <ViewProviderDatum *> (vp) ; vpDatum->setExtents ( bboxDatums ); bboxAction.apply ( vp->getRoot () ); bboxOrigins.extendBy ( bboxAction.getBoundingBox () ); } } // get the bounding box values SbVec3f max = bboxOrigins.getMax(); SbVec3f min = bboxOrigins.getMin(); // obtain an Origin and it's ViewProvider App::Origin* origin = 0; Gui::ViewProviderOrigin* vpOrigin = 0; try { origin = body->getOrigin (); assert (origin); Gui::ViewProvider *vp = Gui::Application::Instance->getViewProvider(origin); if (!vp) { throw Base::Exception ("No view provider linked to the Origin"); } assert ( vp->isDerivedFrom ( Gui::ViewProviderOrigin::getClassTypeId () ) ); vpOrigin = static_cast <Gui::ViewProviderOrigin *> ( vp ); } catch (const Base::Exception &ex) { Base::Console().Error ("%s\n", ex.what() ); return; } // calculate the desired origin size Base::Vector3d size; for (uint_fast8_t i=0; i<3; i++) { size[i] = std::max ( fabs ( max[i] ), fabs ( min[i] ) ); if (size[i] < Precision::Confusion() ) { size[i] = Gui::ViewProviderOrigin::defaultSize(); } } vpOrigin->Size.setValue ( size*1.2 ); }
void paintGL() { const SbViewportRegion vp = view->getSoRenderManager()->getViewportRegion(); SbVec2s size = vp.getViewportSizePixels(); glDisable(GL_LIGHTING); glViewport(0, 0, size[0], size[1]); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, fbObject->texture()); glColor3f(1.0, 1.0, 1.0); GLfloat s = size[0] / GLfloat(fbObject->size().width()); GLfloat t = size[1] / GLfloat(fbObject->size().height()); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0); glTexCoord2f(s, 0.0); glVertex2f(1.0, -1.0); glTexCoord2f(s, t); glVertex2f(1.0, 1.0); glTexCoord2f(0.0, t); glVertex2f(-1.0, 1.0); glEnd(); if (rubberBandIsShown) { glMatrixMode(GL_PROJECTION); glOrtho(0, size[0], size[1], 0, 0, 100); glMatrixMode(GL_MODELVIEW); glDisable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLineWidth(4.0); glColor4f(1.0f, 1.0f, 1.0f, 0.2f); glRecti(rubberBandCorner1.x(), rubberBandCorner1.y(), rubberBandCorner2.x(), rubberBandCorner2.y()); glColor4f(1.0, 1.0, 0.0, 0.5); glLineStipple(3, 0xAAAA); glEnable(GL_LINE_STIPPLE); glBegin(GL_LINE_LOOP); glVertex2i(rubberBandCorner1.x(), rubberBandCorner1.y()); glVertex2i(rubberBandCorner2.x(), rubberBandCorner1.y()); glVertex2i(rubberBandCorner2.x(), rubberBandCorner2.y()); glVertex2i(rubberBandCorner1.x(), rubberBandCorner2.y()); glEnd(); glLineWidth(1.0); glDisable(GL_LINE_STIPPLE); glDisable(GL_BLEND); } glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); }
void NaviCubeImplementation::drawNaviCube(bool pickMode) { // initializes stuff here when we actually have a context // FIXME actually now that we have Qt5, we could probably do this earlier (as we do not need the opengl context) if (!m_NaviCubeInitialised) { QtGLWidget* gl = static_cast<QtGLWidget*>(m_View3DInventorViewer->viewport()); if (gl == NULL) return; initNaviCube(gl); m_NaviCubeInitialised = true; } SoCamera* cam = m_View3DInventorViewer->getSoRenderManager()->getCamera(); if (!cam) return; handleResize(); // Store GL state. glPushAttrib(GL_ALL_ATTRIB_BITS); GLfloat depthrange[2]; glGetFloatv(GL_DEPTH_RANGE, depthrange); GLdouble projectionmatrix[16]; glGetDoublev(GL_PROJECTION_MATRIX, projectionmatrix); glDepthMask(GL_TRUE); glDepthRange(0.0, 1.0); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDisable(GL_LIGHTING); //glDisable(GL_BLEND); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glTexEnvf(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE); glDepthMask(GL_TRUE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glShadeModel(GL_SMOOTH); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CCW); glAlphaFunc( GL_GREATER, 0.25); glEnable( GL_ALPHA_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); const float NEARVAL = 0.1f; const float FARVAL = 10.0f; const float dim = NEARVAL * float(tan(M_PI / 8.0))*1.2; glFrustum(-dim, dim, -dim, dim, NEARVAL, FARVAL); SbMatrix mx; mx = cam->orientation.getValue(); mx = mx.inverse(); mx[3][2] = -5.0; glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadMatrixf((float*) mx); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); if (pickMode) { glDisable(GL_BLEND); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glShadeModel(GL_FLAT); glDisable(GL_DITHER); glDisable(GL_POLYGON_SMOOTH); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } glClear(GL_DEPTH_BUFFER_BIT); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, (void*) m_VertexArray.data()); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, m_TextureCoordArray.data()); if (!pickMode) { // Draw the axes glDisable(GL_TEXTURE_2D); float a=1.1f; static GLubyte xbmp[] = { 0x11,0x11,0x0a,0x04,0x0a,0x11,0x11 }; glColor3f(1, 0, 0); glBegin(GL_LINES); glVertex3f(-1 , -1, -1); glVertex3f(+1 , -1, -1); glEnd(); glRasterPos3d(a, -a, -a); glBitmap(8, 7, 0, 0, 0, 0, xbmp); static GLubyte ybmp[] = { 0x04,0x04,0x04,0x04,0x0a,0x11,0x11 }; glColor3f(0, 1, 0); glBegin(GL_LINES); glVertex3f(-1 , -1, -1); glVertex3f(-1 , +1, -1); glEnd(); glRasterPos3d( -a, a, -a); glBitmap(8, 7, 0, 0, 0, 0, ybmp); static GLubyte zbmp[] = { 0x1f,0x10,0x08,0x04,0x02,0x01,0x1f }; glColor3f(0, 0, 1); glBegin(GL_LINES); glVertex3f(-1 , -1, -1); glVertex3f(-1 , -1, +1); glEnd(); glRasterPos3d( -a, -a, a); glBitmap(8, 7, 0, 0, 0, 0, zbmp); glEnable(GL_TEXTURE_2D); } // Draw the cube faces if (pickMode) { for (vector<Face*>::iterator f = m_Faces.begin(); f != m_Faces.end(); f++) { glColor3ub((*f)->m_PickId, 0, 0); glBindTexture(GL_TEXTURE_2D, (*f)->m_PickTextureId); glDrawElements(GL_TRIANGLE_FAN, (*f)->m_VertexCount, GL_UNSIGNED_BYTE, (void*) &m_IndexArray[(*f)->m_FirstVertex]); } } else { for (int pass = 0; pass < 3 ; pass++) { for (vector<Face*>::iterator f = m_Faces.begin(); f != m_Faces.end(); f++) { if (pickMode) { // pick should not be drawn in tree passes glColor3ub((*f)->m_PickId, 0, 0); glBindTexture(GL_TEXTURE_2D, (*f)->m_PickTextureId); } else { if (pass != (*f)->m_RenderPass) continue; QColor& c = (m_HiliteId == (*f)->m_PickId) && (pass < 2) ? m_HiliteColor : (*f)->m_Color; glColor4f(c.redF(), c.greenF(), c.blueF(),c.alphaF()); glBindTexture(GL_TEXTURE_2D, (*f)->m_TextureId); } glDrawElements(GL_TRIANGLE_FAN, (*f)->m_VertexCount, GL_UNSIGNED_BYTE, (void*) &m_IndexArray[(*f)->m_FirstVertex]); } } } // Draw the rotate buttons glEnable(GL_CULL_FACE); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 1, 1, 0, 0, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); for (vector<int>::iterator b = m_Buttons.begin(); b != m_Buttons.end(); b++) { if (pickMode) glColor3ub(*b, 0, 0); else { QColor& c = (m_HiliteId ==(*b)) ? m_HiliteColor : m_ButtonColor; glColor3f(c.redF(), c.greenF(), c.blueF()); } glBindTexture(GL_TEXTURE_2D, m_Textures[*b]); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex3f(0.0f, 1.0f, 0.0f); glTexCoord2f(1, 0); glVertex3f(1.0f, 1.0f, 0.0f); glTexCoord2f(1, 1); glVertex3f(1.0f, 0.0f, 0.0f); glTexCoord2f(0, 1); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); } // Draw the view menu icon if (pickMode) { glColor3ub(TEX_VIEW_MENU_FACE, 0, 0); glBindTexture(GL_TEXTURE_2D, m_Textures[TEX_VIEW_MENU_FACE]); } else { if (m_HiliteId == TEX_VIEW_MENU_FACE) { QColor& c = m_HiliteColor; glColor4f(c.redF(), c.greenF(), c.blueF(),c.alphaF()); glBindTexture(GL_TEXTURE_2D, m_Textures[TEX_VIEW_MENU_FACE]); glBegin(GL_QUADS); // DO THIS WITH VERTEX ARRAYS glTexCoord2f(0, 0); glVertex3f(0.0f, 1.0f, 0.0f); glTexCoord2f(1, 0); glVertex3f(1.0f, 1.0f, 0.0f); glTexCoord2f(1, 1); glVertex3f(1.0f, 0.0f, 0.0f); glTexCoord2f(0, 1); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); } glColor3ub(255,255,255); glBindTexture(GL_TEXTURE_2D, m_Textures[TEX_VIEW_MENU_ICON]); } glBegin(GL_QUADS); // FIXME do this with vertex arrays glTexCoord2f(0, 0); glVertex3f(0.0f, 1.0f, 0.0f); glTexCoord2f(1, 0); glVertex3f(1.0f, 1.0f, 0.0f); glTexCoord2f(1, 1); glVertex3f(1.0f, 0.0f, 0.0f); glTexCoord2f(0, 1); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); glPopMatrix(); // Restore original state. glDepthRange(depthrange[0], depthrange[1]); glMatrixMode(GL_PROJECTION); glLoadMatrixd(projectionmatrix); glPopAttrib(); }
void DefineNodesCallback(void * ud, SoEventCallback * n) { Fem::FemAnalysis *Analysis; if(getConstraintPrerequisits(&Analysis)) return; // show the wait cursor because this could take quite some time Gui::WaitCursor wc; // When this callback function is invoked we must in either case leave the edit mode Gui::View3DInventorViewer* view = reinterpret_cast<Gui::View3DInventorViewer*>(n->getUserData()); view->setEditing(false); view->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), DefineNodesCallback,ud); n->setHandled(); SbBool clip_inner; std::vector<SbVec2f> clPoly = view->getGLPolygon(&clip_inner); if (clPoly.size() < 3) return; if (clPoly.front() != clPoly.back()) clPoly.push_back(clPoly.front()); SoCamera* cam = view->getSoRenderManager()->getCamera(); SbViewVolume vv = cam->getViewVolume(); Gui::ViewVolumeProjection proj(vv); Base::Polygon2D polygon; for (std::vector<SbVec2f>::const_iterator it = clPoly.begin(); it != clPoly.end(); ++it) polygon.Add(Base::Vector2D((*it)[0],(*it)[1])); std::vector<App::DocumentObject*> docObj = Gui::Selection().getObjectsOfType(Fem::FemMeshObject::getClassTypeId()); if(docObj.size() !=1) return; const SMESHDS_Mesh* data = const_cast<SMESH_Mesh*>(dynamic_cast<Fem::FemMeshObject*>(docObj[0])->FemMesh.getValue().getSMesh())->GetMeshDS(); SMDS_NodeIteratorPtr aNodeIter = data->nodesIterator(); Base::Vector3f pt2d; std::set<int> IntSet; while (aNodeIter->more()) { const SMDS_MeshNode* aNode = aNodeIter->next(); Base::Vector3f vec(aNode->X(),aNode->Y(),aNode->Z()); pt2d = proj(vec); if (polygon.Contains(Base::Vector2D(pt2d.x, pt2d.y)) == true) IntSet.insert(aNode->GetID()); } std::stringstream set; set << "["; for(std::set<int>::const_iterator it=IntSet.begin(); it!=IntSet.end(); ++it) if(it==IntSet.begin()) set << *it ; else set << "," << *it ; set << "]"; Gui::Command::openCommand("Place robot"); Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.addObject('Fem::FemSetNodesObject','NodeSet')"); Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.ActiveObject.Nodes = %s",set.str().c_str()); Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().%s.Member = App.activeDocument().%s.Member + [App.activeDocument().NodeSet]",Analysis->getNameInDocument(),Analysis->getNameInDocument()); ////Gui::Command::updateActive(); Gui::Command::commitCommand(); //std::vector<Gui::ViewProvider*> views = view->getViewProvidersOfType(ViewProviderMesh::getClassTypeId()); //if (!views.empty()) { // Gui::Application::Instance->activeDocument()->openCommand("Cut"); // for (std::vector<Gui::ViewProvider*>::iterator it = views.begin(); it != views.end(); ++it) { // ViewProviderMesh* that = static_cast<ViewProviderMesh*>(*it); // if (that->getEditingMode() > -1) { // that->finishEditing(); // that->cutMesh(clPoly, *view, clip_inner); // } // } // Gui::Application::Instance->activeDocument()->commitCommand(); // view->render(); //} }
void MeshSelection::selectGLCallback(void * ud, SoEventCallback * n) { // When this callback function is invoked we must leave the edit mode Gui::View3DInventorViewer* view = reinterpret_cast<Gui::View3DInventorViewer*>(n->getUserData()); MeshSelection* self = reinterpret_cast<MeshSelection*>(ud); self->stopInteractiveCallback(view); n->setHandled(); std::vector<SbVec2f> polygon = view->getGLPolygon(); if (polygon.size() < 3) return; if (polygon.front() != polygon.back()) polygon.push_back(polygon.front()); SbVec3f pnt, dir; view->getNearPlane(pnt, dir); Base::Vector3f point (pnt[0],pnt[1],pnt[2]); Base::Vector3f normal(dir[0],dir[1],dir[2]); std::list<ViewProviderMesh*> views = self->getViewProviders(); for (std::list<ViewProviderMesh*>::iterator it = views.begin(); it != views.end(); ++it) { ViewProviderMesh* vp = static_cast<ViewProviderMesh*>(*it); std::vector<unsigned long> faces; const Mesh::MeshObject& mesh = static_cast<Mesh::Feature*>((*it)->getObject())->Mesh.getValue(); const MeshCore::MeshKernel& kernel = mesh.getKernel(); // simply get all triangles under the polygon SoCamera* cam = view->getSoRenderManager()->getCamera(); SbViewVolume vv = cam->getViewVolume(); Gui::ViewVolumeProjection proj(vv); vp->getFacetsFromPolygon(polygon, proj, true, faces); if (self->onlyVisibleTriangles) { const SbVec2s& sz = view->getSoRenderManager()->getViewportRegion().getWindowSize(); short width,height; sz.getValue(width,height); std::vector<SbVec2s> pixelPoly = view->getPolygon(); SbBox2s rect; for (std::vector<SbVec2s>::iterator it = pixelPoly.begin(); it != pixelPoly.end(); ++it) { const SbVec2s& p = *it; rect.extendBy(SbVec2s(p[0],height-p[1])); } std::vector<unsigned long> rf; rf.swap(faces); std::vector<unsigned long> vf = vp->getVisibleFacetsAfterZoom (rect, view->getSoRenderManager()->getViewportRegion(), view->getSoRenderManager()->getCamera()); // get common facets of the viewport and the visible one std::sort(vf.begin(), vf.end()); std::sort(rf.begin(), rf.end()); std::back_insert_iterator<std::vector<unsigned long> > biit(faces); std::set_intersection(vf.begin(), vf.end(), rf.begin(), rf.end(), biit); } // if set filter out all triangles which do not point into user direction if (self->onlyPointToUserTriangles) { std::vector<unsigned long> screen; screen.reserve(faces.size()); MeshCore::MeshFacetIterator it_f(kernel); for (std::vector<unsigned long>::iterator it = faces.begin(); it != faces.end(); ++it) { it_f.Set(*it); if (it_f->GetNormal() * normal > 0.0f) { screen.push_back(*it); } } faces.swap(screen); } if (self->addToSelection) vp->addSelection(faces); else vp->removeSelection(faces); } view->redraw(); }
void ViewProviderScattered::cut(const std::vector<SbVec2f>& picked, Gui::View3DInventorViewer &Viewer) { // create the polygon from the picked points Base::Polygon2D cPoly; for (std::vector<SbVec2f>::const_iterator it = picked.begin(); it != picked.end(); ++it) { cPoly.Add(Base::Vector2D((*it)[0],(*it)[1])); } // get a reference to the point feature Points::Feature* fea = static_cast<Points::Feature*>(pcObject); const Points::PointKernel& points = fea->Points.getValue(); SoCamera* pCam = Viewer.getSoRenderManager()->getCamera(); SbViewVolume vol = pCam->getViewVolume(); // search for all points inside/outside the polygon std::vector<unsigned long> removeIndices; removeIndices.reserve(points.size()); unsigned long index = 0; for (Points::PointKernel::const_iterator jt = points.begin(); jt != points.end(); ++jt, ++index) { SbVec3f pt(jt->x,jt->y,jt->z); // project from 3d to 2d vol.projectToScreen(pt, pt); if (cPoly.Contains(Base::Vector2D(pt[0],pt[1]))) removeIndices.push_back(index); } if (removeIndices.empty()) return; // nothing needs to be done //Remove the points from the cloud and open a transaction object for the undo/redo stuff Gui::Application::Instance->activeDocument()->openCommand("Cut points"); // sets the points outside the polygon to update the Inventor node fea->Points.removeIndices(removeIndices); std::map<std::string,App::Property*> Map; pcObject->getPropertyMap(Map); for (std::map<std::string,App::Property*>::iterator it = Map.begin(); it != Map.end(); ++it) { Base::Type type = it->second->getTypeId(); if (type == Points::PropertyNormalList::getClassTypeId()) { static_cast<Points::PropertyNormalList*>(it->second)->removeIndices(removeIndices); } else if (type == Points::PropertyGreyValueList::getClassTypeId()) { static_cast<Points::PropertyGreyValueList*>(it->second)->removeIndices(removeIndices); } else if (type == App::PropertyColorList::getClassTypeId()) { //static_cast<App::PropertyColorList*>(it->second)->removeIndices(removeIndices); const std::vector<App::Color>& colors = static_cast<App::PropertyColorList*>(it->second)->getValues(); if (removeIndices.size() > colors.size()) break; std::vector<App::Color> remainValue; remainValue.reserve(colors.size() - removeIndices.size()); std::vector<unsigned long>::iterator pos = removeIndices.begin(); for (std::vector<App::Color>::const_iterator jt = colors.begin(); jt != colors.end(); ++jt) { unsigned long index = jt - colors.begin(); if (pos == removeIndices.end()) remainValue.push_back( *jt ); else if (index != *pos) remainValue.push_back( *jt ); else ++pos; } static_cast<App::PropertyColorList*>(it->second)->setValues(remainValue); } } // unset the modified flag because we don't need the features' execute() to be called Gui::Application::Instance->activeDocument()->commitCommand(); fea->purgeTouched(); }