root/OpenSceneGraph/branches/OpenSceneGraph-3.2/src/osgPlugins/dxf/ReaderWriterDXF.cpp @ 14038

Revision 14038, 5.2 kB (checked in by robert, 4 days ago)

Added shaders to support experimental shader based displacement mapping technique osgTerrain::ShaderTerrain?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* dxfReader for OpenSceneGraph  Copyright (C) 2005 by GraphArchitecture ( grapharchitecture.com )
2 * Programmed by Paul de Repentigny <pdr@grapharchitecture.com>
3 *
4 * OpenSceneGraph is (C) 2004 Robert Osfield
5 *
6 * This library is provided as-is, without support of any kind.
7 *
8 * Read DXF docs or OSG docs for any related questions.
9 *
10 * You may contact the author if you have suggestions/corrections/enhancements.
11 */
12#include <osgDB/Registry>
13#include <osgDB/ReadFile>
14#include <osgDB/FileUtils>
15#include <osgDB/FileNameUtils>
16
17#include <map>
18#include <iostream>
19#include <utility>
20
21#include <string>
22#include <sstream>
23#include <string.h>
24
25#include "dxfFile.h"
26#include "DXFWriterNodeVisitor.h"
27
28using namespace osg;
29using namespace osgDB;
30using namespace std;
31
32
33class ReaderWriterdxf : public osgDB::ReaderWriter
34{
35public:
36    ReaderWriterdxf()
37    {
38        supportsExtension("dxf","Autodesk DXF format");
39    }
40
41    virtual const char* className() { return "Autodesk DXF Reader/Writer"; }
42    virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) const;
43
44
45    virtual WriteResult writeObject(const osg::Object& obj,const std::string& fileName,const Options* options=NULL) const
46    {
47        const osg::Node* node = dynamic_cast<const osg::Node*>(&obj);
48        if (node)
49            return writeNode(*node, fileName, options);
50        else
51            return WriteResult(WriteResult::FILE_NOT_HANDLED);
52    }
53
54
55    virtual WriteResult writeObject(const osg::Object& obj,std::ostream& fout,const Options* options=NULL) const
56    {
57        const osg::Node* node = dynamic_cast<const osg::Node*>(&obj);
58        if (node)
59            return writeNode(*node, fout, options);
60        else
61            return WriteResult(WriteResult::FILE_NOT_HANDLED);
62    }
63
64    virtual WriteResult writeNode(const osg::Node& node,std::ostream& fout,const Options* =NULL) const
65    {
66
67
68        DXFWriterNodeVisitor nv(fout);
69
70        (const_cast<osg::Node*>(&node))->accept(nv); // first pass is to get all node names and types -> layers
71
72        if ( nv.writeHeader(node.getBound()) ) {
73            (const_cast<osg::Node*>(&node))->accept(nv);  // second pass outputs data
74            nv.writeFooter();
75        }
76
77        return WriteResult(WriteResult::FILE_SAVED);
78    }
79
80    virtual WriteResult writeNode(const osg::Node& node,const std::string& fileName,const Options* options =NULL) const
81    {
82        if (!acceptsExtension(osgDB::getFileExtension(fileName)))
83            return WriteResult(WriteResult::FILE_NOT_HANDLED);
84
85        osgDB::ofstream f(fileName.c_str());
86
87        if (!f.is_open() ) {
88            return WriteResult(WriteResult::ERROR_IN_WRITING_FILE);
89        }
90        DXFWriterNodeVisitor nv(f);
91
92        (const_cast<osg::Node*>(&node))->accept(nv); // first pass is to get all node names and types -> layers
93
94        if ( nv.writeHeader(node.getBound()) ) {
95            (const_cast<osg::Node*>(&node))->accept(nv);  // second pass outputs data
96            nv.writeFooter();
97        }
98
99        return WriteResult(WriteResult::FILE_SAVED);
100    }
101
102protected:
103};
104
105// register with Registry to instantiate the above reader/writer.
106REGISTER_OSGPLUGIN(dxf, ReaderWriterdxf)
107
108
109// read file and convert to OSG.
110osgDB::ReaderWriter::ReadResult
111ReaderWriterdxf::readNode(const std::string& filename, const osgDB::ReaderWriter::Options* options) const
112{
113    std::string ext = osgDB::getFileExtension(filename);
114    if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
115
116    // extract accuracy options if available
117    if (options) {
118        bool useAccuracy=false;  // if we specify accuracy of curve rendering or not
119        double maxError=0.0;     // if useAccuracy - the accuracy (max deviation) from the arc
120        bool improveAccuracyOnly=false; // if true only use the given accuracy if it would improve the curve compared to the previous implementation
121                                        // Thus you can ensure that large curves get rendered better but small ones don't get worse
122
123        std::string optionsstring=options->getOptionString();
124
125        size_t accstart=optionsstring.find("Accuracy(");
126        if (accstart != std::string::npos) {
127            const char* start=optionsstring.c_str() + accstart + strlen("Accuracy(");
128            if (sscanf(start,"%lf",&maxError)==1) useAccuracy=true;
129        }
130        if (useAccuracy) {
131            // Option to only use the new accuracy code when it would improve on the accuracy of the old method
132            if (optionsstring.find("ImproveAccuracyOnly") != std::string::npos) {
133                improveAccuracyOnly=true;
134            }
135            // Pull out the initial dxfArc copy from the registry and set accuracy there.
136            // When actual dxfArcs/Circles are created they will inherit these parameters from the exemplar
137            dxfEntity::getRegistryEntity("ARC")->setAccuracy(true,maxError,improveAccuracyOnly);
138            dxfEntity::getRegistryEntity("CIRCLE")->setAccuracy(true,maxError,improveAccuracyOnly);
139        } // accuracy options exists
140    } // options exist
141
142
143    // Open
144    dxfFile df(filename);
145    if (df.parseFile()) {
146        // convert to OSG
147        osg::Group* osg_top = df.dxf2osg();
148        return (osg_top);
149    }
150    return ReadResult::FILE_NOT_HANDLED;
151}
Note: See TracBrowser for help on using the browser.