root/OpenSceneGraph/trunk/src/osgPlugins/p3d/ReaderWriterPaths.cpp @ 13041

Revision 13041, 8.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
2 *
3 * This software is open source and may be redistributed and/or modified under
4 * the terms of the GNU General Public License (GPL) version 2.0.
5 * The full license is in LICENSE.txt file included with this distribution,.
6 *
7 * This software is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * include LICENSE.txt for more details.
11*/
12
13#include <osg/Notify>
14#include <osg/io_utils>
15
16#include <osgDB/ReaderWriter>
17#include <osgDB/FileNameUtils>
18#include <osgDB/FileUtils>
19#include <osgDB/Registry>
20
21#include <osgWidget/PdfReader>
22
23#include <osgPresentation/SlideShowConstructor>
24#include <osgPresentation/AnimationMaterial>
25
26#include <stdio.h>
27#include <string.h>
28#include <stdlib.h>
29
30#include <osgDB/XmlParser>
31
32#include <sstream>
33#include <iostream>
34
35
36/**
37 * OpenSceneGraph plugin wrapper/converter.
38 */
39class ReaderWriterPaths : public osgDB::ReaderWriter
40{
41public:
42    ReaderWriterPaths()
43    {
44        supportsExtension("material","Material animation Ascii file format");
45        supportsExtension("path","Animation path Ascii file format");
46        supportsExtension("pivot_path","Animation pivot path Ascii file format");
47        supportsExtension("rotation_path","Animation rotation path Ascii file format");
48    }
49
50    virtual const char* className() const
51    {
52        return "Path Reader/Writer";
53    }
54
55    virtual bool acceptsExtension(const std::string& extension) const
56    {
57        return osgDB::equalCaseInsensitive(extension,"material") ||
58               osgDB::equalCaseInsensitive(extension,"path") ||
59               osgDB::equalCaseInsensitive(extension,"pivot_path") ||
60               osgDB::equalCaseInsensitive(extension,"rotation_path");
61    }
62
63    virtual osgDB::ReaderWriter::ReadResult readObject(const std::string& fileName, const osgDB::Options* options) const;
64
65    virtual osgDB::ReaderWriter::ReadResult readObject(std::istream& fin, const osgDB::Options* options) const;
66
67    virtual osgDB::ReaderWriter::ReadResult read_material(std::istream& fin, const osgDB::Options* options) const;
68    virtual osgDB::ReaderWriter::ReadResult read_path(std::istream& fin, const osgDB::Options* options) const;
69    virtual osgDB::ReaderWriter::ReadResult read_pivot_path(std::istream& fin, const osgDB::Options* options) const;
70    virtual osgDB::ReaderWriter::ReadResult read_rotation_path(std::istream& fin, const osgDB::Options* options) const;
71};
72
73// now register with Registry to instantiate the above
74// reader/writer.
75REGISTER_OSGPLUGIN(paths, ReaderWriterPaths)
76
77osgDB::ReaderWriter::ReadResult ReaderWriterPaths::readObject(const std::string& file, const osgDB::Options* options) const
78{
79
80
81    std::string ext = osgDB::getLowerCaseFileExtension(file);
82    if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
83
84    OSG_INFO<<"ReaderWriterPaths::readObject("<<file<<")"<<std::endl;
85
86    std::string fileName = osgDB::findDataFile( file, options );
87    if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
88
89    OSG_INFO<<"  Found path file :"<<fileName<<std::endl;
90
91    // code for setting up the database path so that internally referenced file are searched for on relative paths.
92    osg::ref_ptr<osgDB::ReaderWriter::Options> local_opt = options ? static_cast<osgDB::ReaderWriter::Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
93    local_opt->setPluginStringData("filename",fileName);
94
95    osgDB::ifstream input(fileName.c_str());
96
97    return readObject(input, local_opt.get());
98}
99
100osgDB::ReaderWriter::ReadResult ReaderWriterPaths::readObject(std::istream& fin, const osgDB::Options* options) const
101{
102    OSG_INFO<<"ReaderWriterPaths::readObject(std::istream& fin"<<std::endl;
103
104    if (!options) return ReadResult::FILE_NOT_HANDLED;
105    if (!fin) return ReadResult::ERROR_IN_READING_FILE;
106
107
108    std::string filename = options->getPluginStringData("filename");
109
110    std::string ext = osgDB::getLowerCaseFileExtension(filename);
111
112    OSG_INFO<<"   filename found in options: "<<filename<<"  extension="<<ext<<std::endl;
113
114
115    if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
116
117
118    if      (ext=="path") return read_path(fin, options);
119    else if (ext=="material") return read_material(fin, options);
120    else if (ext=="pivot_path") return read_pivot_path(fin, options);
121    else if (ext=="rotation_path") return read_rotation_path(fin, options);
122
123    return ReadResult::FILE_NOT_HANDLED;
124}
125
126osgDB::ReaderWriter::ReadResult ReaderWriterPaths::read_material(std::istream& fin, const osgDB::Options* options) const
127{
128    osg::ref_ptr<osgPresentation::AnimationMaterial> animationMaterial = new osgPresentation::AnimationMaterial;
129    animationMaterial->read(fin);
130
131    return animationMaterial.get();
132}
133
134osgDB::ReaderWriter::ReadResult ReaderWriterPaths::read_path(std::istream& fin, const osgDB::Options* options) const
135{
136    osg::ref_ptr<osg::AnimationPath> animation = new osg::AnimationPath;
137    animation->read(fin);
138    return animation.get();
139}
140
141osgDB::ReaderWriter::ReadResult ReaderWriterPaths::read_pivot_path(std::istream& fin, const osgDB::Options* options) const
142{
143    osg::ref_ptr<osg::AnimationPath> animation = new osg::AnimationPath;
144
145    while (!fin.eof())
146    {
147        double time;
148        osg::Vec3 pivot;
149        osg::Vec3 position;
150        float scale;
151        osg::Quat rotation;
152        fin >> time >> pivot.x() >> pivot.y() >> pivot.z() >> position.x() >> position.y() >> position.z() >> rotation.x() >> rotation.y() >> rotation.z() >> rotation.w() >> scale;
153        if(!fin.eof())
154        {
155            osg::Matrix SR = osg::Matrix::scale(scale,scale,scale)*
156                                osg::Matrixf::rotate(rotation);
157
158            osg::Matrix invSR;
159            invSR.invert(SR);
160
161            position += (invSR*pivot)*SR;
162
163            animation->insert(time,osg::AnimationPath::ControlPoint(position,rotation,osg::Vec3(scale,scale,scale)));
164        }
165    }
166
167    return animation.get();
168}
169
170struct RotationPathData
171{
172    RotationPathData():
173        time(0.0),
174        scale(1.0f),
175        azim(0.0f),
176        elevation(0.0f) {}
177
178    double time;
179    osg::Vec3 pivot;
180    osg::Vec3 position;
181    float scale;
182    float azim;
183    float elevation;
184
185    void addToPath(osg::AnimationPath* animation) const
186    {
187        osg::Quat Rx, Rz, rotation;
188
189        Rx.makeRotate(osg::DegreesToRadians(elevation),1.0f,0.0f,0.0f);
190        Rz.makeRotate(osg::DegreesToRadians(azim),0.0f,0.0f,1.0f);
191        rotation = Rz * Rx; // note, I believe this is the wrong way round, but I had to put it in this order to fix the Quat properly.
192
193        osg::Matrix SR = osg::Matrix::scale(scale,scale,scale)*
194                         osg::Matrixf::rotate(rotation);
195
196        osg::Matrix invSR;
197        invSR.invert(SR);
198
199        osg::Vec3 local_position = position + (invSR*pivot)*SR;
200
201        animation->insert(time,osg::AnimationPath::ControlPoint(local_position,rotation,osg::Vec3(scale,scale,scale)));
202    }
203
204};
205
206osgDB::ReaderWriter::ReadResult ReaderWriterPaths::read_rotation_path(std::istream& fin, const osgDB::Options* options) const
207{
208    osg::ref_ptr<osg::AnimationPath> animation = new osg::AnimationPath;
209
210    RotationPathData prevValue;
211    bool first = true;
212    while (!fin.eof())
213    {
214        RotationPathData currValue;
215        fin >> currValue.time >> currValue.pivot.x() >> currValue.pivot.y() >> currValue.pivot.z() >> currValue.position.x() >> currValue.position.y() >> currValue.position.z() >> currValue.azim >> currValue.elevation >> currValue.scale;
216
217        if(!fin.eof())
218        {
219
220            if (!first)
221            {
222
223                unsigned int num = 20;
224                float dr = 1.0f/(float)num;
225                float r=dr;
226                for(unsigned int i=0;
227                    i<num;
228                    ++i, r+=dr)
229                {
230                    RotationPathData localValue;
231                    localValue.time = currValue.time *r + prevValue.time * (1.0f-r);
232                    localValue.pivot = currValue.pivot *r + prevValue.pivot * (1.0f-r);
233                    localValue.position = currValue.position *r + prevValue.position * (1.0f-r);
234                    localValue.scale = currValue.scale *r + prevValue.scale * (1.0f-r);
235                    localValue.azim = currValue.azim *r + prevValue.azim * (1.0f-r);
236                    localValue.elevation = currValue.elevation *r + prevValue.elevation * (1.0f-r);
237
238                    localValue.addToPath(animation.get());
239                }
240            }
241            else
242            {
243                currValue.addToPath(animation.get());
244            }
245            prevValue = currValue;
246            first = false;
247        }
248
249    }
250    OSG_NOTICE<<"finished"<<std::endl;
251
252    return animation.get();
253}
Note: See TracBrowser for help on using the browser.