问题背景
第一篇笔记中只能看看没有意思,得自己交互试试看才能记得深刻。为以后不重复写交互,增加了一个基类。
增加交互窗口后,第一篇笔记的效果如下图
动图为:
在x,y,z对应的输入框中输入坐标,就可在界面中重新渲染小球的位置。
TutorialWindow 自动生成一个可交互的窗口:
#pragma once
#include <string>
#include "../imgui/imgui.h"
class TutorialWindow {
public:
virtual ~TutorialWindow() = default;
void renderTutorialWindow(Handle(AIS_InteractiveContext)& context) {
// 初始化窗口
initTutorialWin();
// 渲染
ImGui::Begin("Tutorial Window", &showTutorialWindow); // 打开一个新的ImGui窗口
renderContent(context);
ImGui::End();
}
void openTutorialWindow() { showTutorialWindow = true; }
void closeTutorialWindow() { showTutorialWindow = false; }
protected:
virtual void renderContent(Handle(AIS_InteractiveContext)& context) = 0; // 纯虚函数,要求子类实现窗口的内容
void initTutorialWin() const {
static bool initialized = false; // 保证只初始化一次
if (!initialized) {
ImVec2 windowPos(1200, 500); // 设置窗口左上角位置
ImVec2 windowSize(600, 400); // 设置窗口大小
ImGui::SetNextWindowPos(windowPos);
ImGui::SetNextWindowSize(windowSize);
initialized = true;
}
}
private:
bool showTutorialWindow = false; // 控制窗口显示的标志
};
Step 2
只需要在红框初增加对应代码就可生成窗口:
class MDPointAndVector001 : public BaseScene, public VisSceneComponents, public TutorialWindow {
public:
MDPointAndVector001() { openTutorialWindow(); }
void displayScene(Handle(V3d_View)& view, Handle(AIS_InteractiveContext)& context) override {
//<创建起始点
// 1. 使用默认构造函数创建一个零点
gp_Pnt point1;
// 2. 使用三维坐标创建一个点
gp_Pnt point2(1.0, 2.0, 3.0);
// 3. 使用另外一个点来构造新点
gp_Pnt point3 = point2;
//<创建Vector
// 1. 使用默认构造函数创建一个零向量
gp_Vec vec1;
// 2. 使用两个点之间的差值创建向量
gp_Pnt p1(2.0, 3.0, 4.0);
gp_Pnt p2(1.0, 3.0, 5.0);
gp_Vec vec2(p1, p2); // vec2 = p2 - p1
// 3. 使用三维坐标直接创建一个向量
gp_Vec vec3(7.0, 8.0, 9.0);
//<向量计算演示
// 计算两个向量之间的加法
gp_Vec vec4 = vec2 + vec3; // vec4 = vec2 + vec3
//<可视化
visPoint(point2, context); // 可视化点
visVector(gp_Pnt(7.0, 8.0, 9.0), vec2, context, makeColor(0.4157, 0.5020, 0.7255)); // 可视化向量vec2
visVector(point1, vec3, context, makeColor(0.5333, 0.6196, 0.4510)); // 可视化向量加法结果
visVector(point1, vec4, context, makeColor(0.6627, 0.2902, 0.2902)); // 可视化向量叉积结果
//<可视化教学窗口
renderTutorialWindow(context);
}
// 教学窗口内容
void renderContent(Handle(AIS_InteractiveContext)& context) override {
// 使用ImGui创建滑动条来控制x、y、z坐标
ImGui::SliderFloat("X", &x, -10.0f, 10.0f); // 控制X坐标
ImGui::SliderFloat("Y", &y, -10.0f, 10.0f); // 控制Y坐标
ImGui::SliderFloat("Z", &z, -10.0f, 10.0f); // 控制Z坐标
// 更新点的位置
visPnt.SetX(x);
visPnt.SetY(y);
visPnt.SetZ(z);
// 创建球体并可视化
ImGui::Spacing();
if (ImGui::Button("Create a new gp_Pnt!")) {
visPoint(visPnt, context); // 按钮点击时创建新的点
}
ImGui::Spacing();
ImGui::Spacing();
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "### gp_Pnt and gp_Vector Overview");
ImGui::Text("gp_Pnt and gp_Vector are fundamental classes in the Open Cascade (OCC) geometry modeling library, used to represent points and vectors in geometric space.");
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "### gp_Pnt Class:");
ImGui::Text("gp_Pnt is used to represent a point in three-dimensional space, defined by three coordinates (x, y, z). This point typically indicates a position in 3D space.");
ImGui::BulletText("Constructor: Create a point with given coordinates:");
ImGui::Text("gp_Pnt(double X, double Y, double Z);");
ImGui::BulletText("Default Constructor: Create the origin (0, 0, 0):");
ImGui::Text("gp_Pnt();");
ImGui::BulletText("Member Functions:");
ImGui::BulletText("X(), Y(), Z() - Get the x, y, and z coordinates of the point.");
ImGui::BulletText("Distance(const gp_Pnt&) - Calculate the distance between the current point and another point.");
ImGui::BulletText("Transformed(const gp_Trsf&) - Apply a transformation matrix to the point.");
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "### gp_Vector Class:");
ImGui::Text("gp_Vector is used to represent a vector in three-dimensional space. A vector has both direction and magnitude, typically representing the relative displacement between two points or motion in space.");
ImGui::BulletText("Constructor: Create a vector with given coordinates:");
ImGui::Text("gp_Vector(double X, double Y, double Z);");
ImGui::BulletText("Alternatively, create a vector using two points:");
ImGui::Text("gp_Vector(const gp_Pnt& P1, const gp_Pnt& P2);");
ImGui::BulletText("Member Functions:");
ImGui::BulletText("Magnitude() - Get the magnitude (size) of the vector.");
ImGui::BulletText("Normalize() - Normalize the vector to a unit vector.");
ImGui::BulletText("Crossed(const gp_Vector&) - Calculate the cross product with another vector.");
ImGui::BulletText("Dot(const gp_Vector&) - Calculate the dot product with another vector.");
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "### Relationship and Differences:");
ImGui::Text("gp_Pnt represents a point with specific coordinates, while gp_Vector represents a vector, which has direction and magnitude, often used to express the relationship or changes between two points.");
ImGui::Text("In Open Cascade, gp_Pnt and gp_Vector are commonly used together. For example, a gp_Vector can represent the direction and distance from one point to another, or it can be applied to a gp_Pnt to yield a new point.");
}
private:
gp_Pnt visPnt;
float x = 10.0f;
float y = 10.0f;
float z = 10.0f;
};