You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
8.2 KiB
C++

#ifndef _HUTILITY_H
#define _HUTILITY_H
#include <osg/Node>
#include <osg/NodeVisitor>
#include <osgViewer/Viewer>
#include "Assist/SceneGraphDef.h"
namespace OSGView
{
//窗口尺寸变化回调
class XJWindowReSizeCallBack : public osgGA::GUIEventHandler
{
public:
XJWindowReSizeCallBack() {}
~XJWindowReSizeCallBack() {}
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa,
osg::Object* node, osg::NodeVisitor* nv)
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
if (!viewer) return false;
if(ea.getEventType() == osgGA::GUIEventAdapter::RESIZE)
{
const osg::GraphicsContext::Traits* trait = viewer->getCamera()->getGraphicsContext()->getTraits();
osg::Camera* camera = dynamic_cast<osg::Camera*>(node);
if(camera)
{
camera->setProjectionMatrixAsOrtho2D(0, trait->width, 0, trait->height);
camera->setViewport(0, 0, trait->width, trait->height);
viewer->requestRedraw();
}
}
return false;
}
};
//节点查找访问器
/***************************************/
class FindNodeVisitor : public osg::NodeVisitor
{
public:
//构造函数,参数为需要查找的节点名
FindNodeVisitor(const std::string& searchName);
//重载apply()方法
virtual void apply(osg::Node& node);
virtual void apply(osg::Geode& geode);
virtual void apply(osg::Transform& transform);
virtual void apply(osg::Camera& camera);
virtual void apply(osg::Group& group);
//设置要查找的节点名
void setNameToFind(const std::string& searchName);
//得到查找节点向量的第一个节点
osg::Node* getFirst() ;
//定义一个节点向量
typedef std::vector<osg::Node*> NodeList;
//得到查找节点向量
NodeList& getNodeList()
{
return foundNodeList;
}
private:
//节点名
std::string searchForName;
//用来保存查找的节点
NodeList foundNodeList;
};
class HUtility
{
public:
HUtility() {}
~HUtility() {}
static int getProjectionViewVoulme(osg::Camera* cam, double& l, double& r, double& b, double& t, double& nz, double& fz);
/*!
InsertRectangle inserts a 2d rectangle as a osg Polyline.
\param seg The segment name into which the rectangle should be inserted.
\param x0 X-value of vertex 1 of the rectangle.
\param y0 Y-value of vertex 1 of the rectangle.
\param x1 X-value of vertex 3 of the rectangle.
\param y1 Y-value of vertex 3 of the rectangle.
\param z The z coordinate shared by vertext 1 and 3.
\param fill Pass true to create a filled rectangle.
\return The key to the new created rectangle.
*/
static osg::Node* InsertRectangle(float x0, float y0, float x1, float y1, float z = 0.0f,
bool fill = false, osg::Vec4 color = White_Color, float size = 2.0);
/*!
InsertLine inserts a line as a osg line.
\param firstPt the start pt of line
\param lastPt the end pt of line
\color, the color attribute of line
*/
static osg::Node* InsertPoint(const osg::Vec3& pt, osg::Vec4 color = Red_Color, float pointsize = 1.0f);
/*!
InsertSphere inserts a sphere as a osg sphere.
\param firstPt the start pt of line
\param lastPt the end pt of line
\color, the color attribute of line
*/
static osg::Node* InsertSphere(const osg::Vec3& center, float rad = 1.0, osg::Vec4 color = Red_Color);
/*!
InsertLine inserts a line as a osg line.
\param firstPt the start pt of line
\param lastPt the end pt of line
\color, the color attribute of line
*/
static osg::Node* InsertLine(const osg::Vec3& firstPt, const osg::Vec3& lastPt, osg::Vec4 color = White_Color);
/*!
InsertLineDrawable inserts a line Drawable as a osg line.
\param firstPt the start pt of line
\param lastPt the end pt of line
\color, the color attribute of line
*/
static osg::Drawable* InsertLineDrawable(const osg::Vec3& firstPt, const osg::Vec3& lastPt, osg::Vec4 color = White_Color, float lineWidth = 1.0);
/*!
InserDashLineDrawable inserts a dash line Drawable as a osg line.
\param firstPt the start pt of line
\param lastPt the end pt of line
\color, the color attribute of line
*/
static osg::Drawable* InserDashLineDrawable(const osg::Vec3& firstPt, const osg::Vec3& lastPt, osg::Vec4 color = White_Color, const char* name = NULL);
static osg::Drawable* InsertPolylineDrawable(std::vector<osg::Vec3>& polylines, osg::Vec4 color = White_Color, const char* name = NULL);
/*!
InsertPlane inserts a plane geode.
*/
static osg::Geode* InsertPlane();
/*!
InsertText inserts a line Drawable as a osg line.
*/
static osg::Drawable* InsertText(const osg::Vec3& position, std::string charText, float textSize = 1.0);
static osg::Drawable* InsertText(const osg::Vec3& position, wchar_t* charText, float textSize /* = 1.0 */);
/*!
InsertArrow inserts a line Drawable as a osg line.
*/
static osg::Drawable* InsertArrow(float arrowSize = 1.0f, float lenght = 2.0);
static osg::Node* InsertArrow(const osg::Vec3& pnt, const osg::Vec3& nor);
//Insert point
static osg::Drawable* InsertPointDrawable(const osg::Vec3& pt, osg::Vec4 color = Red_Color, float pointsize = 1.0f);
//insert circle int the origin of world coordinate
static osg::Drawable* InsertCircle(float radius, unsigned int numSegments, float w, const osg::Vec4& color);
static osg::Geode* InsertAxis(float size);
/*!
InsertPolyline inserts a line as a osg line.
*/
static osg::Node* InsertPolyline(std::vector<osg::Vec3>& polylines, int state = 0 );
/*!
InsertPolyline inserts a line as a osg line.
*/
static osg::Node* InsertPolyline(int num, osg::Vec3* polylines);
/*!
InsertPolyline inserts a line as a osg line.
*/
static osg::Node* InsertPointSet(std::vector<osg::Vec3>& pointSet, osg::Vec4 color = Red_Color, float pointSize = 10.0);
/*!
Project the points form window into world space
*/
static void ConvertCoordinate(osgViewer::Viewer* pView, const char* inChar,
const osg::Vec3& inPosition,const char* outChar, osg::Vec3& outPosition);
//
static void AdjustPositionToPlane(osgViewer::Viewer* pView, osg::Vec3& pos, const osg::Vec3& plane_point);
//compute project point on plane
static void ProjectPointOnPlane(osg::Vec3 plane_dir, const osg::Vec3& plane_point, osg::Vec3& projectPoint);
//compute the intersection between line and plane
static void IntersectionSegmentPlane(const osg::Vec3& plane_dir, const osg::Vec3& plane_point,
const osg::Vec3& line_dir, const osg::Vec3& line_point, osg::Vec3& intersection) ;
/*!
FindWindowSpaceCamera inserts Or get a 2d Camera which Orientate Z Axis.
\param seg The segment name into which the rectangle should be inserted.
\param x0 X-value of vertex 1 of the rectangle.
\return The node of the camera finded.
*/
static osg::Camera* FindWindowSpaceCamera(osgViewer::Viewer* aView);
/*!
FindNodeInSceneGraph get a group has a name.
\param seg The segment name of Group.
\param .
\return The group which user need.
*/
static osg::Group* GetGroupInSceneGraph(const char* name, osgViewer::Viewer* aView);
/*!
FindNodeInSceneGraph get a group has a name.
\param seg The segment name of Group.
\param .
\return The group which user need.
*/
static osg::Group* GetGroupByName(const char* name, osg::Group* pGroup);
/*!
FindNodeByName get a group has a name.
\param seg The segment name of Group.
\param .
\return The group which user need.
*/
static osg::Node* FindNodeByName(const char* name, osg::Group* pGroup);
/*!
RemoveNodeByName remove a node with name from pGroup.
\param seg The segment name of node.
\param .
\ return true for success. If Node is not found then return false
*/
static bool RemoveNodeByName(const char* name, osg::Group* pGroup);
/*!
RemoveAllChildren remove all children from pGroup.
*/
static bool RemoveAllChildren(osg::Group* pGroup);
/*!
RemoveDrawable remove the drawable from geode.
*/
static bool RemoveDrawable(const char* name, osg::Geode* pGeode);
/*!
GetDrawable get the drawable from geode.
*/
static osg::Drawable* GetDrawable(const char* name, osg::Geode* pGeode);
static bool MakeRotationByAxes(const osg::Vec3& xdir, const osg::Vec3& ydir, const osg::Vec3& zdir,
osg::Vec3& rotateAxis, double& angle,const char* priorityOrder = "ZXY");
};
} //end namespace
#endif