Browse Source

使用tinyxml2

list:
- Point
- Geometry
divide_struct_def_imp
郑敬润 1 year ago
parent
commit
1813d42b00
  1. 15
      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

15
CMakeLists.txt

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

2
include/BranchPoint.h

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

96
include/Geometry.h

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

14
include/Point.h

@ -3,15 +3,15 @@
#ifdef MY_LIB_SHARED_BUILD #ifdef MY_LIB_SHARED_BUILD
#ifdef _WIN32 #ifdef _WIN32
#ifdef MY_LIB_EXPORTS #ifdef MY_LIB_EXPORTS
#define POINT_LIB_API __declspec(dllexport) #define LIB_API __declspec(dllexport)
#else #else
#define POINT_LIB_API __declspec(dllimport) #define LIB_API __declspec(dllimport)
#endif // MY_LIB_EXPORTS #endif // MY_LIB_EXPORTS
#else #else
#define POINT_LIB_API #define LIB_API
#endif // _WIN32 #endif // _WIN32
#else #else
#define POINT_LIB_API #define LIB_API
#endif // MY_LIB_SHARED_BUILD #endif // MY_LIB_SHARED_BUILD
@ -22,11 +22,11 @@
using namespace std; 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 x, y, z; // 坐标
double dx, dy, dz; // 方向 double dx, dy, dz; // 方向

46
include/ReadXML.h

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

6
include/stdafx.h

@ -5,9 +5,7 @@
#pragma once #pragma once
#pragma comment(lib, "tinyxml.lib") // #pragma comment(lib, "tinyxml.lib")
#pragma once
#define Word Microsoft::Office::Interop::Word #define Word Microsoft::Office::Interop::Word
@ -21,7 +19,7 @@
#endif #endif
#include <ctype.h> #include <ctype.h>
#include <iostream> #include <iostream>
#include <tinyxml.h> #include <tinyxml2.h>
// #include <tinystr.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 struct Readexcel
{ {
vector<string> slist; 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 equal root node then return
if (0 == strcmp(strNodeName, pRootEle->Value())) if (0 == strcmp(strNodeName, pRootEle->Value()))
{ {
return pRootEle; return pRootEle;
} }
TiXmlElement *pEle = pRootEle; tinyxml2::XMLElement *pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement()) for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{ {
// recursive find sub node return node pointer // recursive find sub node return node pointer
if (0 != strcmp(pEle->Value(), strNodeName)) if (0 != strcmp(pEle->Value(), strNodeName))
{ {
TiXmlElement *res = GetNodePointerByName(pEle, strNodeName); tinyxml2::XMLElement *res = GetNodePointerByName(pEle, strNodeName);
if (res != NULL) if (res != NULL)
return res; return res;
} }
@ -52,20 +52,20 @@ struct Readexcel
{ {
slist.clear(); slist.clear();
TiXmlDocument *pDoc = new TiXmlDocument(); tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
pDoc->LoadFile(xml.c_str()); 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()) for (pEle = row; pEle; pEle = pEle->NextSiblingElement())
{ {
// recursive find sub node return node pointer // recursive find sub node return node pointer
TiXmlElement *cell; tinyxml2::XMLElement *cell;
string s[7]; string s[7];
int i = 0; int i = 0;
for (cell = pEle->FirstChildElement(); cell; cell = cell->NextSiblingElement()) 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; 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; ofstream ofs, oendpoint;
ofs.open("result.txt", ios::out); ofs.open("result.txt", ios::out);

Loading…
Cancel
Save