Revision: 50651
          http://brlcad.svn.sourceforge.net/brlcad/?rev=50651&view=rev
Author:   d_rossberg
Date:     2012-05-24 12:12:41 +0000 (Thu, 24 May 2012)
Log Message:
-----------
a raw (simple ASCII bag of triangles) to g geometry converter

Modified Paths:
--------------
    brlcad/trunk/src/conv/CMakeLists.txt
    brlcad/trunk/src/conv/Makefile.am

Added Paths:
-----------
    brlcad/trunk/src/conv/raw/
    brlcad/trunk/src/conv/raw/Bot.cpp
    brlcad/trunk/src/conv/raw/Bot.h
    brlcad/trunk/src/conv/raw/CMakeLists.txt
    brlcad/trunk/src/conv/raw/Makefile.am
    brlcad/trunk/src/conv/raw/RegionList.cpp
    brlcad/trunk/src/conv/raw/RegionList.h
    brlcad/trunk/src/conv/raw/raw-g.cpp

Modified: brlcad/trunk/src/conv/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/conv/CMakeLists.txt        2012-05-24 03:39:26 UTC (rev 
50650)
+++ brlcad/trunk/src/conv/CMakeLists.txt        2012-05-24 12:12:41 UTC (rev 
50651)
@@ -4,6 +4,7 @@
 
 add_subdirectory(iges)
 add_subdirectory(intaval)
+add_subdirectory(raw)
 add_subdirectory(vdeck)
 
 set(CONV_INCLUDE_DIRS

Modified: brlcad/trunk/src/conv/Makefile.am
===================================================================
--- brlcad/trunk/src/conv/Makefile.am   2012-05-24 03:39:26 UTC (rev 50650)
+++ brlcad/trunk/src/conv/Makefile.am   2012-05-24 12:12:41 UTC (rev 50651)
@@ -1,5 +1,6 @@
 IGESDIR=iges
 INTAVALDIR=intaval
+RAWDIR=raw
 
 EXTRA_DIST = \
        CMakeLists.txt \
@@ -33,11 +34,13 @@
 SUBDIRS = \
        ${IGESDIR} \
        ${INTAVALDIR} \
+       ${RAWDIR} \
        vdeck
 
 DIST_SUBDIRS = \
        iges \
        intaval \
+       raw \
        vdeck
 
 EXTRA_DIST = \

Added: brlcad/trunk/src/conv/raw/Bot.cpp
===================================================================
--- brlcad/trunk/src/conv/raw/Bot.cpp                           (rev 0)
+++ brlcad/trunk/src/conv/raw/Bot.cpp   2012-05-24 12:12:41 UTC (rev 50651)
@@ -0,0 +1,168 @@
+/*                         B O T . C P P
+ * BRL-CAD
+ *
+ * Copyright (c) 2012 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file Bot.cpp
+ *
+ * RAW geometry file to BRL-CAD converter:
+ * bag of triangles intermediate data structure implementation
+ *
+ *  Origin -
+ *     IABG mbH (Germany)
+ */
+
+#include "Bot.h"
+
+
+Bot::Bot(void) : m_name(), m_thickness(0.), m_vertices(), m_faces() {}
+
+
+void Bot::setName(const std::string& value)
+{
+    m_name = value;
+}
+
+
+void Bot::setThickness(double value)
+{
+    m_thickness = value;
+}
+
+
+size_t Bot::addPoint(point_t& point)
+{
+    size_t ret = m_vertices.size() / 3;
+
+    // search for duplicate vertex
+    for(size_t i = 0; i < (m_vertices.size() / 3); ++i) {
+       if (NEAR_ZERO(m_vertices[i * 3] - point[X], SMALL_FASTF) &&
+           NEAR_ZERO(m_vertices[i * 3 + 1] - point[Y], SMALL_FASTF) &&
+           NEAR_ZERO(m_vertices[i * 3 + 2] - point[Z], SMALL_FASTF)) {
+           ret = i;
+           break;
+       }
+    }
+
+    if (ret == (m_vertices.size() / 3)) {
+       // add a new vertex
+       m_vertices.push_back(point[X]);
+       m_vertices.push_back(point[Y]);
+       m_vertices.push_back(point[Z]);
+    }
+
+    return ret; // return index of vertex
+}
+
+
+void Bot::addTriangle(int a,
+                     int b,
+                     int c)
+{
+    // add a new triangle
+    m_faces.push_back(a);
+    m_faces.push_back(b);
+    m_faces.push_back(c);
+}
+
+
+const std::string& Bot::name(void) const
+{
+    return m_name;
+}
+
+
+void Bot::write(rt_wdb* wdbp) const
+{
+    if (m_thickness > 0.) {
+       if ((m_faces.size() / 3) > 0)
+           writePlate(wdbp);
+       else
+           std::cout << "Ignore degenerated BOT " << m_name.c_str() << '\n';
+    }
+    else {
+       if ((m_faces.size() / 3) > 3)
+           writeSolid(wdbp);
+       else
+           std::cout << "Ignore degenerated BOT " << m_name.c_str() << '\n';
+    }
+}
+
+
+void Bot::writeSolid(rt_wdb* wdbp) const
+{
+    fastf_t* vertices = static_cast<fastf_t*>(malloc(m_vertices.size() * 
sizeof(fastf_t)));
+    int*     faces    = static_cast<int*>(malloc(m_faces.size() * 
sizeof(int)));
+
+    for (size_t i = 0; i < m_vertices.size(); ++i)
+       vertices[i] = m_vertices[i];
+
+    for (size_t i = 0; i < m_faces.size(); ++i)
+       faces[i] = m_faces[i];
+
+    mk_bot(wdbp,
+          m_name.c_str(),
+          RT_BOT_SOLID,
+          RT_BOT_UNORIENTED,
+          0,
+          m_vertices.size() / 3,
+          m_faces.size() / 3,
+          vertices,
+          faces,
+          0,
+          0);
+
+    free(vertices);
+    free(faces);
+}
+
+
+void Bot::writePlate(rt_wdb* wdbp) const
+{
+    fastf_t* vertices    = static_cast<fastf_t*>(malloc(m_vertices.size() * 
sizeof(fastf_t)));
+    int*     faces       = static_cast<int*>(malloc(m_faces.size() * 
sizeof(int)));
+    fastf_t* thicknesses = static_cast<fastf_t*>(malloc(m_faces.size() * 
sizeof(fastf_t) / 3));
+    bu_bitv* faceModes   = bu_bitv_new(m_faces.size() / 3);
+
+    for (size_t i = 0; i < m_vertices.size(); ++i)
+       vertices[i] = m_vertices[i];
+
+    for (size_t i = 0; i < m_faces.size(); ++i)
+       faces[i] = m_faces[i];
+
+    for (size_t i = 0; i < (m_faces.size() / 3); ++i)
+       thicknesses[i] = m_thickness;
+
+    bu_bitv_clear(faceModes);
+
+    mk_bot(wdbp,
+          m_name.c_str(),
+          RT_BOT_PLATE,
+          RT_BOT_UNORIENTED,
+          0,
+          m_vertices.size() / 3,
+          m_faces.size() / 3,
+          vertices,
+          faces,
+          thicknesses,
+          faceModes);
+
+    free(vertices);
+    free(faces);
+    free(thicknesses);
+    bu_bitv_free(faceModes);
+}


Property changes on: brlcad/trunk/src/conv/raw/Bot.cpp
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/conv/raw/Bot.h
===================================================================
--- brlcad/trunk/src/conv/raw/Bot.h                             (rev 0)
+++ brlcad/trunk/src/conv/raw/Bot.h     2012-05-24 12:12:41 UTC (rev 50651)
@@ -0,0 +1,63 @@
+/*                         B O T . H
+ * BRL-CAD
+ *
+ * Copyright (c) 2012 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file Bot.h
+ *
+ * RAW geometry file to BRL-CAD converter:
+ * bag of triangles intermediate data structure declaration
+ *
+ *  Origin -
+ *     IABG mbH (Germany)
+ */
+
+#ifndef BOT_INCLUDED
+#define BOT_INCLUDED
+
+#include <vector>
+
+#include "wdb.h"
+
+
+class Bot {
+public:
+    Bot(void);
+
+    void               setName(const std::string& value);
+    void               setThickness(double value);
+    size_t             addPoint(point_t& point);
+    void               addTriangle(int  a,
+                                  int  b,
+                                  int  c);
+
+    const std::string& name(void) const;
+    void               write(rt_wdb* wdbp) const;
+
+
+private:
+    std::string          m_name;
+    double               m_thickness;
+    std::vector<fastf_t> m_vertices;
+    std::vector<int>     m_faces;
+
+    void writeSolid(rt_wdb* wdbp) const;
+    void writePlate(rt_wdb* wdbp) const;
+};
+
+
+#endif //BOT_INCLUDED


Property changes on: brlcad/trunk/src/conv/raw/Bot.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/conv/raw/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/conv/raw/CMakeLists.txt                            (rev 0)
+++ brlcad/trunk/src/conv/raw/CMakeLists.txt    2012-05-24 12:12:41 UTC (rev 
50651)
@@ -0,0 +1,34 @@
+set(RAW_INCLUDE_DIRS
+  ${BU_INCLUDE_DIRS}
+  ${RT_INCLUDE_DIRS}
+  ${WDB_INCLUDE_DIRS}
+  )
+LIST(REMOVE_DUPLICATES RAW_INCLUDE_DIRS)
+include_directories(${RAW_INCLUDE_DIRS})
+
+if(MSVC)
+  add_definitions(
+    -DON_DLL_IMPORTS
+    )
+endif(MSVC)
+
+set(rawg_SRCS
+  Bot.cpp
+  raw-g.cpp
+  RegionList.cpp
+  )
+BRLCAD_ADDEXEC(raw-g "${rawg_SRCS}" "libwdb;librt;libbu")
+
+set(RAW_IGNORE_FILES
+  Bot.h
+  RegionList.h
+  )
+CMAKEFILES(${RAW_IGNORE_FILES})
+CMAKEFILES(Makefile.am)
+
+# Local Variables:
+# tab-width: 8
+# mode: cmake
+# indent-tabs-mode: t
+# End:
+# ex: shiftwidth=2 tabstop=8


Property changes on: brlcad/trunk/src/conv/raw/CMakeLists.txt
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/conv/raw/Makefile.am
===================================================================
--- brlcad/trunk/src/conv/raw/Makefile.am                               (rev 0)
+++ brlcad/trunk/src/conv/raw/Makefile.am       2012-05-24 12:12:41 UTC (rev 
50651)
@@ -0,0 +1,25 @@
+
+bin_PROGRAMS = raw-g
+
+AM_CPPFLAGS = ${TCL_CPPFLAGS} ${OPENNURBS_CPPFLAGS}
+
+raw_g_SOURCES = \
+       Bot.cpp \
+       raw-g.cpp \
+       RegionList.cpp
+
+raw_g_LDADD = ${WDB} ${WDB_LIBS} ${BU} ${BU_LIBS}
+
+noinst_HEADERS = Bot.h \
+       RegionList.h
+
+EXTRA_DIST = \
+       CMakeLists.txt
+
+DEPS=src/libwdb src/librt
+
+include $(top_srcdir)/misc/Makefile.defs
+
+FAST_OBJECTS = \
+       $(raw_g_OBJECTS) \
+       $(bin_PROGRAMS)


Property changes on: brlcad/trunk/src/conv/raw/Makefile.am
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/conv/raw/RegionList.cpp
===================================================================
--- brlcad/trunk/src/conv/raw/RegionList.cpp                            (rev 0)
+++ brlcad/trunk/src/conv/raw/RegionList.cpp    2012-05-24 12:12:41 UTC (rev 
50651)
@@ -0,0 +1,87 @@
+/*                     R E G I O N L I S T . C P P
+ * BRL-CAD
+ *
+ * Copyright (c) 2012 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file RegionList.cpp
+ *
+ * RAW geometry file to BRL-CAD converter:
+ * regions intermediate data structure implementation
+ *
+ *  Origin -
+ *     IABG mbH (Germany)
+ */
+
+#include "RegionList.h"
+
+
+RegionList::RegionList(void) : m_list() {}
+
+
+Bot& RegionList::addRegion(const std::string& name)
+{
+    Bot& ret = m_list[name];
+
+    std::string botName(name);
+    botName += ".bot";
+    ret.setName(botName);
+
+    return ret;
+}
+
+
+void RegionList::create(rt_wdb* wdbp)
+{
+    wmember allRegions;
+    BU_LIST_INIT(&allRegions.l);
+
+    for (std::map<std::string, Bot>::iterator it = m_list.begin();
+        it != m_list.end();
+        ++it) {
+       it->second.write(wdbp);
+
+       wmember regionContent;
+       BU_LIST_INIT(&regionContent.l);
+       mk_addmember(it->second.name().c_str(), &regionContent.l, 0, 
WMOP_UNION);
+
+       int id = toValue(it->first.c_str());
+
+       mk_lrcomb(wdbp,
+                 it->first.c_str(),
+                 &regionContent,
+                 1,
+                 "plastic",
+                 "sh=4 sp=0.5 di=0.5 re=0.1",
+                 0,
+                 id,
+                 0,
+                 0,
+                 100,
+                 0);
+       mk_addmember(it->first.c_str(), &allRegions.l, 0, WMOP_UNION);
+       mk_freemembers(&regionContent.l);
+    }
+
+    mk_lfcomb(wdbp, "all.g", &allRegions, 0);
+    mk_freemembers(&allRegions.l);
+}
+
+
+void RegionList::printStat(void) const
+{
+    std::cout << "Over all size: " << m_list.size() << '\n';
+}


Property changes on: brlcad/trunk/src/conv/raw/RegionList.cpp
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/conv/raw/RegionList.h
===================================================================
--- brlcad/trunk/src/conv/raw/RegionList.h                              (rev 0)
+++ brlcad/trunk/src/conv/raw/RegionList.h      2012-05-24 12:12:41 UTC (rev 
50651)
@@ -0,0 +1,64 @@
+/*                     R E G I O N L I S T . H
+ * BRL-CAD
+ *
+ * Copyright (c) 2012 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file RegionList.h
+ *
+ * RAW geometry file to BRL-CAD converter:
+ * regions intermediate data structure declaration
+ *
+ *  Origin -
+ *     IABG mbH (Germany)
+ */
+
+#ifndef REGIONLIST_INCLUDED
+#define REGIONLIST_INCLUDED
+
+#include <map>
+#include <sstream>
+
+#include "Bot.h"
+
+
+class RegionList {
+public:
+    RegionList(void);
+
+    Bot& addRegion(const std::string& name);
+
+    void create(rt_wdb* wdbp);
+
+    void printStat(void) const;
+
+private:
+    std::map<std::string, Bot> m_list;
+};
+
+
+inline fastf_t toValue(const char* string)
+{
+    fastf_t            ret;
+    std::istringstream buffer(string);
+
+    buffer >> ret;
+
+    return ret;
+}
+
+
+#endif // REGIONLIST_INCLUDED


Property changes on: brlcad/trunk/src/conv/raw/RegionList.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Added: brlcad/trunk/src/conv/raw/raw-g.cpp
===================================================================
--- brlcad/trunk/src/conv/raw/raw-g.cpp                         (rev 0)
+++ brlcad/trunk/src/conv/raw/raw-g.cpp 2012-05-24 12:12:41 UTC (rev 50651)
@@ -0,0 +1,155 @@
+/*                         R A W - G . C P P
+ * BRL-CAD
+ *
+ * Copyright (c) 2012 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file raw-g.cpp
+ *
+ * RAW geometry file to BRL-CAD converter:
+ * main function
+ *
+ *  Origin -
+ *        IABG mbH (Germany)
+ */
+
+#include <cassert>
+
+#include "RegionList.h"
+
+
+static std::vector<std::string> readLine(std::istream& is)
+{
+    std::vector<std::string> ret;
+    std::string              temp;
+
+    for (;;) {
+       if (!is)
+           break;
+
+       char a;
+       is.get(a);
+
+       if (a == '\n') {
+           if (temp.size() > 0) {
+               ret.push_back(temp);
+               temp.clear();
+           }
+
+           break;
+       }
+
+       if ((a == ' ') || (a == '\t') || (a == '\r')) {
+           if (temp.size() > 0) {
+               ret.push_back(temp);
+               temp.clear();
+           }
+       }
+       else
+           temp += a;
+    }
+
+    return ret;
+}
+
+
+int main(int   argc,
+        char* argv[])
+{
+    int        ret = 0;
+    RegionList regions;
+
+    if (argc < 3) {
+       std::cout << "Usage: " << argv[0] << " <RAW file> <BRL-CAD file>" << 
std::endl;
+       ret = 1;
+    }
+    else {
+       std::ifstream is(argv[1]);
+
+       if (!is.is_open()) {
+           std::cout << "Error reading RAW file" << std::endl;
+           ret = 1;
+       }
+       else {
+           struct rt_wdb* wdbp = wdb_fopen(argv[2]);
+           std::string title = "Converted from ";
+           title += argv[1];
+
+           mk_id(wdbp, title.c_str());
+
+           std::vector<std::string> nameLine = readLine(is);
+
+           while (is && !is.eof()) {
+               if (nameLine.size() == 0) {
+                   nameLine = readLine(is);
+                   continue;
+               }
+
+               std::cout << "Read: " << nameLine[0].c_str() << '\n';
+               assert(nameLine[0].size() > 0);
+
+               Bot& bot = regions.addRegion(nameLine[0]);
+
+               if (nameLine.size() > 1) {
+                   size_t thicknessIndex = nameLine[1].find("thickf=");
+
+                   if (thicknessIndex != std::string::npos) {
+                       std::string thickf = nameLine[1].substr(thicknessIndex 
+ 7);
+
+                       if (thickf.size() > 0)
+                           bot.setThickness(toValue(thickf.c_str()));
+                       else
+                           std::cout << "Missing thickness in " << 
nameLine[0].c_str() << '\n';
+                   }
+               }
+
+               std::vector<std::string> triangleLine = readLine(is);
+
+               while (is && (triangleLine.size() == 9)) {
+                   point_t point;
+
+                   point[X] = toValue(triangleLine[0].c_str());
+                   point[Y] = toValue(triangleLine[1].c_str());
+                   point[Z] = toValue(triangleLine[2].c_str());
+                   int a = bot.addPoint(point);
+
+                   point[X] = toValue(triangleLine[3].c_str());
+                   point[Y] = toValue(triangleLine[4].c_str());
+                   point[Z] = toValue(triangleLine[5].c_str());
+                   int b = bot.addPoint(point);
+
+                   point[X] = toValue(triangleLine[6].c_str());
+                   point[Y] = toValue(triangleLine[7].c_str());
+                   point[Z] = toValue(triangleLine[8].c_str());
+                   int c = bot.addPoint(point);
+
+                   bot.addTriangle(a, b, c);
+
+                   triangleLine = readLine(is);
+               }
+
+               nameLine = triangleLine;
+           }
+
+           regions.create(wdbp);
+           wdb_close(wdbp);
+       }
+    }
+
+    regions.printStat();
+
+    return ret;
+}


Property changes on: brlcad/trunk/src/conv/raw/raw-g.cpp
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to