Browse Source

使用tinyxml2

list:
- Point
- Geometry
divide_struct_def_imp
郑敬润 1 year ago
parent
commit
1813d42b00
  1. 21
      CMakeLists.txt
  2. 2
      include/BranchPoint.h
  3. 96
      include/Geometry.h
  4. 14
      include/Point.h
  5. 46
      include/ReadXML.h
  6. 6
      include/stdafx.h
  7. 38
      lib/Geometry.cpp
  8. 18
      src/xmlsql.cpp

21
CMakeLists.txt

@ -42,7 +42,7 @@ elseif (WIN32)
list(PREPEND CMAKE_PREFIX_PATH D:/SOURCE/Dependencies) # Windows
set(CMAKE_TOOLCHAIN_FILE "D:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake") # vcpkg
include(${CMAKE_TOOLCHAIN_FILE})
find_package(tinyxml CONFIG REQUIRED)
find_package(tinyxml2 CONFIG REQUIRED)
elseif (UNIX)
list(PREPEND CMAKE_PREFIX_PATH /home/yony/PROJECTS/Dependencies) # Linux
find_package(tinyxml CONFIG REQUIRED)
@ -80,24 +80,17 @@ add_executable(
# COMMAND ${CMAKE_COMMAND} -E copy_directory
# ${CMAKE_SOURCE_DIR}/lib/$(IntDir)/WireRouting.dll $(outdir)
# )
file(COPY ${CMAKE_SOURCE_DIR}/lib/Debug/WireRouting.dll DESTINATION ${PROJECT_BINARY_DIR}/Debug)
# file(COPY ${CMAKE_SOURCE_DIR}/lib/Debug/WireRouting.dll DESTINATION ${PROJECT_BINARY_DIR}/Debug)
#------------REQUIRED_5.2 CMake ------------#
# target_link_libraries(${PROJECT_NAME} PUBLIC {lib_name1} {lib_name1} ...)
# CMakeLists.txt
# target_include_directories <lib_name>/include <LIB_NAME>_INCLUDE_DIRS
if (WIN32)
target_link_libraries(
${PROJECT_NAME} PUBLIC
unofficial-tinyxml::unofficial-tinyxml
)
else()
target_link_libraries(
${PROJECT_NAME} PUBLIC
/opt/homebrew/Cellar/tinyxml/2.6.2/lib/libtinyxml.dylib
)
endif()
target_link_libraries(
${PROJECT_NAME} PUBLIC
tinyxml2::tinyxml2
)
target_link_libraries(
${PROJECT_NAME} PUBLIC
@ -114,8 +107,6 @@ target_include_directories(
${PROJECT_NAME} PUBLIC
${PROJECT_BINARY_DIR} # config.h.in build
${CMAKE_SOURCE_DIR}/include
${TINYXML_INCLUDE_DIR} # tinyxml
${TINYXML_INCLUDE_DIRS} # pkg-config tinyxml dir
)
# TODO: MSVC msvc_19.29_cxx11_32_md_debugdll/lib

2
include/BranchPoint.h

@ -7,6 +7,8 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <assert.h>
using namespace std;
// 分支点数据类型

96
include/Geometry.h

@ -1,5 +1,19 @@
#pragma once
#ifdef MY_LIB_SHARED_BUILD
#ifdef _WIN32
#ifdef MY_LIB_EXPORTS
#define LIB_API __declspec(dllexport)
#else
#define LIB_API __declspec(dllimport)
#endif // MY_LIB_EXPORTS
#else
#define LIB_API
#endif // _WIN32
#else
#define LIB_API
#endif // MY_LIB_SHARED_BUILD
#include <numeric>
#include <limits>
#include <cmath>
@ -47,37 +61,25 @@ public:
return a;
}
// Vec3 operator+(const Vec3 &other) const {
// return {a + other.a, b + other.b, c + other.c};
// }
Vec3 operator+(const Vec3 &other) const
{
return Vec3(a + other.a, b + other.b, c + other.c);
} // VS2008
}
// Vec3 operator/(const float w) const {
// return {a / w, b / w, c / w};
// }
Vec3 operator/(const float w) const
{
return Vec3(a / w, b / w, c / w);
} // VS2008
}
// Vec3 operator-(const Vec3 &other) const {
// return {a - other.a, b - other.b, c - other.c};
// }
Vec3 operator-(const Vec3 &other) const
{
return Vec3(a - other.a, b - other.b, c - other.c);
} // VS2008
}
// Vec3 operator*(const float &w) const {
// return {a * w, b * w, c * w};
// }
Vec3 operator*(const float &w) const
{
return Vec3(a * w, b * w, c * w);
} // VS2008
}
float length() const
{
@ -89,9 +91,6 @@ public:
return *this / length();
}
// Vec3 cross(const Vec3 &other) const {
// return {b * other.c - c * other.b, c * other.a - a * other.c, a * other.b - b * other.a};
// }
Vec3 cross(const Vec3 &other) const
{
return Vec3(b * other.c - c * other.b, c * other.a - a * other.c, a * other.b - b * other.a);
@ -106,76 +105,43 @@ public:
typedef Vec3<float> Vec3f;
typedef Vec3<unsigned int> Vec3u;
class Mesh
extern "C" class LIB_API Mesh
{
public:
std::vector<Vec3f> vertices;
std::vector<Vec3u> indices;
};
class AABB
extern "C" class LIB_API AABB
{
public:
Vec3f min, max;
/*
AABB()
:min(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(),std::numeric_limits<float>::max()),
max(std::numeric_limits<float>::min(), std::numeric_limits<float>::min(),std::numeric_limits<float>::min())
{}
*/
AABB()
{
min = Vec3f(1e5, 1e5, 1e5);
max = Vec3f(0, 0, 0);
}
AABB();
};
class LineSegment
extern "C" class LIB_API LineSegment
{
public:
Vec3f start;
Vec3f end;
float length;
Vec3f dir;
// LineSegment(const Vec3f &s, const Vec3f &e) : start(s), end(e) {
// length = (end - start).length();
// dir = (end - start).norm();
// }
LineSegment(const Vec3f &s, const Vec3f &e) : start(s), end(e)
{
calculate();
}
LineSegment(const Vec3f &s, const Vec3f &e);
float getLength() const
{
return length;
}
float getLength() const;
Vec3f getDir() const
{
return dir;
}
Vec3f getDir() const;
// private:
float length;
Vec3f dir;
void calculate()
{ // VS2008
Vec3f diff = end - start; // VS2008
length = diff.length(); // VS2008
dir = diff.norm(); // VS2008
} // VS2008
void calculate();
};
struct FaceCenterComparator
extern "C" struct LIB_API FaceCenterComparator
{
const std::vector<Vec3f> &faceCenters; // Ìæ»» YourVectorType ΪÄãµÄÏòÁ¿ÀàÐÍ
int longAxis;
FaceCenterComparator(const std::vector<Vec3f> &centers, int axis)
: faceCenters(centers), longAxis(axis) {}
FaceCenterComparator(const std::vector<Vec3f> &centers, int axis);
bool operator()(const size_t &a, const size_t &b) const
{
return faceCenters[a][longAxis] < faceCenters[b][longAxis];
}
bool operator()(const size_t &a, const size_t &b) const;
};

14
include/Point.h

@ -3,15 +3,15 @@
#ifdef MY_LIB_SHARED_BUILD
#ifdef _WIN32
#ifdef MY_LIB_EXPORTS
#define POINT_LIB_API __declspec(dllexport)
#define LIB_API __declspec(dllexport)
#else
#define POINT_LIB_API __declspec(dllimport)
#define LIB_API __declspec(dllimport)
#endif // MY_LIB_EXPORTS
#else
#define POINT_LIB_API
#define LIB_API
#endif // _WIN32
#else
#define POINT_LIB_API
#define LIB_API
#endif // MY_LIB_SHARED_BUILD
@ -22,11 +22,11 @@
using namespace std;
extern "C" POINT_LIB_API const double eps = 1e-6;
extern "C" LIB_API const double eps = 1e-6;
extern "C" POINT_LIB_API bool Equal(double a, double b);
extern "C" LIB_API bool Equal(double a, double b);
extern "C" struct POINT_LIB_API P
extern "C" struct LIB_API P
{
double x, y, z; // 坐标
double dx, dy, dz; // 方向

46
include/ReadXML.h

@ -7,7 +7,9 @@
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
string ini = "--";
// Őë˝Ĺ
@ -85,11 +87,11 @@ void init_readxml()
cnt = 0;
}
void producePin(TiXmlElement *pin)
void producePin(tinyxml2::XMLElement *pin)
{
pinidmap[pin->Attribute("id")] = pin->Attribute("name");
}
string safegave(TiXmlElement *pEle, const char *t)
string safegave(tinyxml2::XMLElement *pEle, const char *t)
{
if (pEle->Attribute(t) == NULL)
return ini;
@ -103,7 +105,7 @@ string safegave(TiXmlElement *pEle, const char *t)
}
}
TiXmlElement *GetNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
tinyxml2::XMLElement *GetNodePointerByName(tinyxml2::XMLElement *pRootEle, const char *strNodeName)
{
// if equal root node then return
if (0 == strcmp(strNodeName, pRootEle->Value()))
@ -111,13 +113,13 @@ TiXmlElement *GetNodePointerByName(TiXmlElement *pRootEle, const char *strNodeNa
return pRootEle;
}
TiXmlElement *pEle = pRootEle;
tinyxml2::XMLElement *pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
if (0 != strcmp(pEle->Value(), strNodeName))
{
TiXmlElement *res = GetNodePointerByName(pEle, strNodeName);
tinyxml2::XMLElement *res = GetNodePointerByName(pEle, strNodeName);
if (res != NULL)
return res;
}
@ -127,10 +129,10 @@ TiXmlElement *GetNodePointerByName(TiXmlElement *pRootEle, const char *strNodeNa
return NULL;
}
TiXmlElement *GetNextNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
tinyxml2::XMLElement *GetNextNodePointerByName(tinyxml2::XMLElement *pRootEle, const char *strNodeName)
{
// if equal root node then return
TiXmlElement *pEle;
tinyxml2::XMLElement *pEle;
for (pEle = pRootEle->NextSiblingElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
@ -139,7 +141,7 @@ TiXmlElement *GetNextNodePointerByName(TiXmlElement *pRootEle, const char *strNo
}
return NULL;
}
void produceConnector(TiXmlElement *pcon)
void produceConnector(tinyxml2::XMLElement *pcon)
{
Connector con;
con.id = pcon->Attribute("id");
@ -154,8 +156,8 @@ void produceConnector(TiXmlElement *pcon)
connectors[connectorNum] = con;
TiXmlElement *pEle;
TiXmlElement *p;
tinyxml2::XMLElement *pEle;
tinyxml2::XMLElement *p;
for (pEle = pcon->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
if (strcmp(pEle->Value(), "backshell") == 0)
@ -185,14 +187,14 @@ void produceConnector(TiXmlElement *pcon)
}
}
void produceWire(TiXmlElement *pwire)
void produceWire(tinyxml2::XMLElement *pwire)
{
Pin sn, en;
TiXmlElement *pconnection = GetNodePointerByName(pwire, "connection");
tinyxml2::XMLElement *pconnection = GetNodePointerByName(pwire, "connection");
if (pconnection != NULL)
{
sn = pinmap[pconnection->Attribute("pinref")];
TiXmlElement *pconnection2 = GetNextNodePointerByName(pconnection, "connection");
tinyxml2::XMLElement *pconnection2 = GetNextNodePointerByName(pconnection, "connection");
if (pconnection2 != NULL)
en = pinmap[pconnection2->Attribute("pinref")];
else
@ -242,7 +244,7 @@ void produceWire(TiXmlElement *pwire)
wire_node[p] = bd;
}
}
void produce(TiXmlElement *pEle)
void produce(tinyxml2::XMLElement *pEle)
{
if (strcmp(pEle->Value(), "wire") == 0)
produceWire(pEle);
@ -250,7 +252,7 @@ void produce(TiXmlElement *pEle)
produceConnector(pEle);
return;
}
void ProduceNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
void ProduceNodePointerByName(tinyxml2::XMLElement *pRootEle, const char *strNodeName)
{
// if equal root node then return
if (0 == strcmp(strNodeName, pRootEle->Value()))
@ -259,7 +261,7 @@ void ProduceNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
return;
}
TiXmlElement *pEle = pRootEle;
tinyxml2::XMLElement *pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
@ -275,7 +277,7 @@ void ProduceNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
return;
}
void readxml(const char *xml, const char *connectorFile)
void readxml(const string xml, const string connectorFile)
{
init_readxml();
ifstream infile;
@ -299,15 +301,17 @@ void readxml(const char *xml, const char *connectorFile)
mp[fields[0]] = P(atof(fields[1].c_str()), atof(fields[2].c_str()), atof(fields[3].c_str()));
}
TiXmlDocument *pDoc = new TiXmlDocument(xml);
if (!(pDoc->LoadFile()))
tinyxml2::XMLDocument pDoc;
tinyxml2::XMLError error = pDoc.LoadFile(xml.c_str());
if (error != tinyxml2::XMLError::XML_SUCCESS)
return;
// pDoc->Print();
// ťńľĂ¸ůÔŞËŘŁŹź´PersonsĄŁ
// cout<<"connectivity"<<endl<<endl;
TiXmlElement *RootElement = pDoc->RootElement();
TiXmlElement *connectivity = GetNodePointerByName(RootElement, "connectivity");
tinyxml2::XMLElement *RootElement = pDoc.RootElement();
tinyxml2::XMLElement *connectivity = GetNodePointerByName(RootElement, "connectivity");
// cout<<(connectivity->Value())<<endl;
// cout<<(connectivity->Attribute("id")==NULL)<<endl;
ProduceNodePointerByName(connectivity, "connector");

6
include/stdafx.h

@ -5,9 +5,7 @@
#pragma once
#pragma comment(lib, "tinyxml.lib")
#pragma once
// #pragma comment(lib, "tinyxml.lib")
#define Word Microsoft::Office::Interop::Word
@ -21,7 +19,7 @@
#endif
#include <ctype.h>
#include <iostream>
#include <tinyxml.h>
#include <tinyxml2.h>
// #include <tinystr.h>

38
lib/Geometry.cpp

@ -0,0 +1,38 @@
// #include "stdafx.h"
#include "Geometry.h"
AABB::AABB()
{
min = Vec3f(1e5, 1e5, 1e5);
max = Vec3f(0, 0, 0);
}
LineSegment::LineSegment(const Vec3f &s, const Vec3f &e) : start(s), end(e)
{
calculate();
}
float LineSegment::getLength() const
{
return length;
}
Vec3f LineSegment::getDir() const
{
return dir;
}
void LineSegment::calculate()
{
Vec3f diff = end - start;
length = diff.length();
dir = diff.norm();
}
FaceCenterComparator::FaceCenterComparator(const std::vector<Vec3f> &centers, int axis)
: faceCenters(centers), longAxis(axis) {}
bool FaceCenterComparator::operator()(const size_t &a, const size_t &b) const
{
return faceCenters[a][longAxis] < faceCenters[b][longAxis];
}

18
src/xmlsql.cpp

@ -26,20 +26,20 @@ map<P, string> pmp, clipName;
struct Readexcel
{
vector<string> slist;
TiXmlElement *GetNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
tinyxml2::XMLElement *GetNodePointerByName(tinyxml2::XMLElement *pRootEle, const char *strNodeName)
{
// if equal root node then return
if (0 == strcmp(strNodeName, pRootEle->Value()))
{
return pRootEle;
}
TiXmlElement *pEle = pRootEle;
tinyxml2::XMLElement *pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
if (0 != strcmp(pEle->Value(), strNodeName))
{
TiXmlElement *res = GetNodePointerByName(pEle, strNodeName);
tinyxml2::XMLElement *res = GetNodePointerByName(pEle, strNodeName);
if (res != NULL)
return res;
}
@ -52,20 +52,20 @@ struct Readexcel
{
slist.clear();
TiXmlDocument *pDoc = new TiXmlDocument();
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
pDoc->LoadFile(xml.c_str());
TiXmlElement *RootElement = pDoc->RootElement();
tinyxml2::XMLElement *RootElement = pDoc->RootElement();
TiXmlElement *row = GetNodePointerByName(RootElement, "Row");
tinyxml2::XMLElement *row = GetNodePointerByName(RootElement, "Row");
TiXmlElement *pEle;
tinyxml2::XMLElement *pEle;
for (pEle = row; pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
TiXmlElement *cell;
tinyxml2::XMLElement *cell;
string s[7];
int i = 0;
for (cell = pEle->FirstChildElement(); cell; cell = cell->NextSiblingElement())
@ -201,7 +201,7 @@ bool read_OBJ(const string &filename, vector<Vec3f> &vertices, vector<Vec3u> &in
return true;
}
void produceXML(const char *xml, const char *connectorFile, string resultfile)
void produceXML(const string xml, const string connectorFile, string resultfile)
{
ofstream ofs, oendpoint;
ofs.open("result.txt", ios::out);

Loading…
Cancel
Save