Skip to content

Commit

Permalink
Add InteractiveTexturedPlane
Browse files Browse the repository at this point in the history
  • Loading branch information
ncPUMA committed Jan 21, 2024
1 parent fe70acd commit 26db1d4
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 5 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_subdirectory(Arrows)
add_subdirectory(BooleanOps)
add_subdirectory(ShapeToMesh)
add_subdirectory(TexturedShapes)
add_subdirectory(TexturedPlain)

file(COPY Models/ DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/Models")
file(COPY Textures/ DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/Textures")
2 changes: 2 additions & 0 deletions ExamplesBase/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ add_library(ExamplesBase SHARED
Objects/interactivesurfaceplane.cpp
Objects/interactivesurfacerevolution.h
Objects/interactivesurfacerevolution.cpp
Objects/interactivetexturedplane.h
Objects/interactivetexturedplane.cpp
)

target_link_libraries(ExamplesBase PRIVATE Qt5::Widgets)
Expand Down
62 changes: 62 additions & 0 deletions ExamplesBase/Objects/interactivetexturedplane.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
SPDX-FileCopyrightText: 2024 Ilya Pominov <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "interactivetexturedplane.h"

#include <AIS_DisplayMode.hxx>
#include <Graphic3d_Texture2Dplane.hxx>
#include <Prs3d_ShadingAspect.hxx>

namespace ExamplesBase {

class InteractiveTexturedPlanePrivate
{
friend class InteractiveTexturedPlane;

TCollection_AsciiString textureFileName;
};

IMPLEMENT_STANDARD_RTTIEXT(InteractiveTexturedPlane, InteractiveSurfacePlane)

InteractiveTexturedPlane::InteractiveTexturedPlane()
: InteractiveSurfacePlane()
, d(new InteractiveTexturedPlanePrivate)
{
SetDisplayMode(AIS_Shaded);

Attributes()->SetupOwnShadingAspect();
auto shadingAspect = Attributes()->ShadingAspect()->Aspect();
auto material = shadingAspect->FrontMaterial();
material.SetColor(Quantity_NOC_WHITE);
material.SetTransparency(.001);
shadingAspect->SetFrontMaterial(material);
shadingAspect->SetTextureMapOn(true);

setTextureFileName("../Textures/barcode2.png");
}

InteractiveTexturedPlane::~InteractiveTexturedPlane()
{
delete d;
}

TCollection_AsciiString InteractiveTexturedPlane::textureFileName() const
{
return d->textureFileName;
}

void InteractiveTexturedPlane::setTextureFileName(const TCollection_AsciiString &fname)
{
d->textureFileName = fname;
Handle(Graphic3d_Texture2Dplane) texture = new Graphic3d_Texture2Dplane(d->textureFileName);
texture->DisableRepeat();
texture->DisableModulate();

auto shadingAspect = Attributes()->ShadingAspect()->Aspect();
shadingAspect->SetTextureMap(texture);
}

}
34 changes: 34 additions & 0 deletions ExamplesBase/Objects/interactivetexturedplane.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
SPDX-FileCopyrightText: 2024 Ilya Pominov <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/

#ifndef INTERACTIVETEXTUREDPLANE_H
#define INTERACTIVETEXTUREDPLANE_H

#include "interactivesurfaceplane.h"

namespace ExamplesBase {

class InteractiveTexturedPlanePrivate;

class InteractiveTexturedPlane : public InteractiveSurfacePlane
{
DEFINE_STANDARD_RTTIEXT(InteractiveTexturedPlane, InteractiveSurfacePlane)
public:
InteractiveTexturedPlane();
~InteractiveTexturedPlane();

TCollection_AsciiString textureFileName() const;
void setTextureFileName(const TCollection_AsciiString &fname);

private:
InteractiveTexturedPlanePrivate *d;
};

DEFINE_STANDARD_HANDLE(InteractiveTexturedPlane, InteractiveSurfacePlane)

}

#endif // INTERACTIVETEXTUREDPLANE_H
8 changes: 7 additions & 1 deletion ExamplesBase/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TopoDS_Edge findEdgeByPoint(const TopoDS_Shape &shape, const gp_Pnt &localPnt)
return res;
}

gp_Dir getNormal(const TopoDS_Face &face, const gp_Pnt &point)
gp_Dir getNormal(const TopoDS_Face &face, const gp_Pnt &point, gp_Dir *D1U)
{
auto aSurf = BRep_Tool::Surface(face);
Standard_Real u1, u2, v1, v2;
Expand All @@ -67,8 +67,14 @@ gp_Dir getNormal(const TopoDS_Face &face, const gp_Pnt &point)

GeomLProp_SLProps props(aSurf, pUV.X(), pUV.Y(), 1, 0.01);
gp_Dir normal = props.Normal();
if (D1U) {
*D1U = props.D1U();
}
if (face.Orientation() == TopAbs_REVERSED || face.Orientation() == TopAbs_INTERNAL) {
normal.Reverse();
if (D1U) {
D1U->Reverse();
}
}
return normal;
}
Expand Down
2 changes: 1 addition & 1 deletion ExamplesBase/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ExamplesBase {
EXAMPLESBASE_EXPORT TopoDS_Face findFaceByPoint(const TopoDS_Shape &shape, const gp_Pnt &localPnt);
EXAMPLESBASE_EXPORT TopoDS_Edge findEdgeByPoint(const TopoDS_Shape &shape, const gp_Pnt &localPnt);

EXAMPLESBASE_EXPORT gp_Dir getNormal(const TopoDS_Face &face, const gp_Pnt &point);
EXAMPLESBASE_EXPORT gp_Dir getNormal(const TopoDS_Face &face, const gp_Pnt &point, gp_Dir *D1U = nullptr);
EXAMPLESBASE_EXPORT std::vector <gp_Dir> getNormals(const TopoDS_Face &face, const std::vector <gp_Pnt> &points);

}
Expand Down
7 changes: 4 additions & 3 deletions SurfaceColor/coloredshape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ void ColoredShape::Compute(const Handle(PrsMgr_PresentationManager3d) &prsMgr,
}
}
}
Standard_Real aTransparency = Transparency() ;
if (aTransparency > 0.0)

const auto transparency = Transparency();
if (transparency > 0.0)
{
SetTransparency (aTransparency);
SetTransparency(transparency);
}
}
1 change: 1 addition & 0 deletions SurfaceColor/coloredshape.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ColoredShape : public AIS_Shape
void setFaceColor(const TopoDS_Face &face, const Quantity_Color &color);
void resetColors();

protected:
void Compute(const Handle(PrsMgr_PresentationManager3d) &prsMgr,
const Handle(Prs3d_Presentation) &prs,
const Standard_Integer mode) override;
Expand Down
36 changes: 36 additions & 0 deletions TexturedPlain/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.5)

project(TexturedPlain VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(PROJECT_H

)

set(PROJECT_SOURCES
main.cpp
viewport.h
viewport.cpp
)

add_executable(TexturedPlain
${PROJECT_SOURCES}
)

target_include_directories(
TexturedPlain
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../
)

target_link_libraries(TexturedPlain PUBLIC ExamplesBase PRIVATE Qt5::Widgets)

target_link_libraries(TexturedPlain PRIVATE
TKernel TKMath TKService TKV3d TKOpenGl
TKBRep TKIGES TKSTL TKVRML TKSTEP TKSTEPAttr TKSTEP209
TKSTEPBase TKGeomBase TKGeomAlgo TKG3d TKG2d
TKXSBase TKShHealing TKHLR TKTopAlgo TKMesh TKPrim
TKCDF TKBool TKBO TKFillet TKOffset TKLCAF TKCAF TKVCAF
TKBin TKXml TKRWMesh
)
22 changes: 22 additions & 0 deletions TexturedPlain/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
SPDX-FileCopyrightText: 2024 Ilya Pominov <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <QApplication>

#include <ExamplesBase/interactiveobjectsmainwindow.h>

#include "viewport.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

ExamplesBase::InteractiveObjectsMainWindow w;
w.setViewport(new Viewport);
w.setGeometry(600, 360, 1200, 720);
w.show();
return a.exec();
}
123 changes: 123 additions & 0 deletions TexturedPlain/viewport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
SPDX-FileCopyrightText: 2024 Ilya Pominov <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "viewport.h"

#include <QDebug>
#include <QMenu>

#include <AIS_InteractiveContext.hxx>

#include <ExamplesBase/ModelLoader/steploader.h>
#include <ExamplesBase/Objects/interactivetexturedplane.h>
#include <ExamplesBase/utility.h>
#include <gp_QuaternionSLerp.hxx>
#include <TopoDS_Face.hxx>

class ViewportPrivate
{
friend class Viewport;

Handle(AIS_Shape) model;
};

Viewport::Viewport()
: ExamplesBase::InteractiveObjectsViewport()
, d_ptr(new ViewportPrivate)
{
context()->IsoOnPlane(Standard_False);
context()->SetIsoNumber(1);

QMetaObject::invokeMethod(this, [this]() {
const char *path = "../Models/45deg AdjMirr Adapter Left Rev1.STEP";
ExamplesBase::StepLoader loader;
d_ptr->model = new AIS_Shape(loader.load(path));
d_ptr->model->SetDisplayMode(AIS_Shaded);
context()->Display(d_ptr->model, Standard_True);
fitInView();
}, Qt::QueuedConnection);
}

Viewport::~Viewport()
{
delete d_ptr;
}

void Viewport::objectsViewMenuRequest(const Handle(AIS_InteractiveObject) &obj, QMenu &menu)
{
createContextMenu(obj, gp_XYZ(), menu);
}

void Viewport::contextMenuRequest(const Handle(AIS_InteractiveObject) &object,
const gp_XYZ &pickedPoint,
QMenu &menu)
{
createContextMenu(object, pickedPoint, menu);
}

void Viewport::createContextMenu(const Handle(AIS_InteractiveObject) &object,
const gp_XYZ &pickedPoint,
QMenu &menu)
{
if (object && object == d_ptr->model) {
menu.addAction(tr("Plane"), this, [this, object, pickedPoint]() {
const auto shape = d_ptr->model->Shape();
const auto face = ExamplesBase::findFaceByPoint(shape, pickedPoint);
if (face.IsNull()) {
return;
}

Handle(ExamplesBase::InteractiveTexturedPlane) plane = new ExamplesBase::InteractiveTexturedPlane;
plane->setUmax(20.);
plane->setVmax(10.);
plane->setTextureFileName("../Textures/barcode.png");
addToContext(plane, pickedPoint, tr("Texture"), object);

gp_Dir D1U;
const auto normal = ExamplesBase::getNormal(face, pickedPoint, &D1U);
gp_Trsf trsf;
auto xAxis = D1U.Crossed(normal);
auto yAxis = normal.Crossed(xAxis);
gp_Mat mat(xAxis.XYZ(), yAxis.XYZ(), normal.XYZ());
trsf.SetRotationPart(gp_Quaternion(mat));
gp_Pnt pos(pickedPoint);
pos.Translate(gp_Vec(normal) * .1);
pos.Translate(gp_Vec(yAxis) * plane->getVmax() * -1.);
trsf.SetTranslationPart(pos.XYZ());
context()->SetLocation(plane, trsf);
});
return;
}

auto interactive = Handle(ExamplesBase::InteractiveObject)::DownCast(object);
if (interactive) {
menu.addAction(tr("Remove"), this, [this, interactive]() {
removeFromContext(interactive);
});
menu.addSeparator();
if (manipulatorAttachedObject() != interactive) {
menu.addAction(tr("Transform"), this, [this, interactive]() {
showManipulator(interactive);
});
}
if (editorAttachedObject() != interactive) {
menu.addAction(tr("Edit"), this, [this, interactive]() {
showEditor(interactive);
});
}
}

if (manipulatorAttachedObject()) {
menu.addAction(tr("End transform"), this, [this]() {
removeManipulator();
});
}
if (editorAttachedObject()) {
menu.addAction(tr("End edit"), this, [this]() {
removeEditor();
});
}
}
36 changes: 36 additions & 0 deletions TexturedPlain/viewport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
SPDX-FileCopyrightText: 2024 Ilya Pominov <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/

#ifndef VIEWPORT_H
#define VIEWPORT_H

#include <ExamplesBase/Viewport/interactiveobjectsviewport.h>

class ViewportPrivate;

class Viewport : public ExamplesBase::InteractiveObjectsViewport
{
public:
Viewport();
~Viewport();

protected:
void objectsViewMenuRequest(const Handle(AIS_InteractiveObject) &obj, QMenu &menu) final;

void contextMenuRequest(const Handle(AIS_InteractiveObject) &object,
const gp_XYZ &pickedPoint,
QMenu &menu) final;

private:
void createContextMenu(const Handle(AIS_InteractiveObject) &object,
const gp_XYZ &pickedPoint,
QMenu &menu);

private:
ViewportPrivate *const d_ptr;
};

#endif // VIEWPORT_H
Binary file added Textures/barcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Textures/barcode2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 26db1d4

Please sign in to comment.