Field3D
|
00001 //----------------------------------------------------------------------------// 00002 00003 /* 00004 * Copyright (c) 2009 Sony Pictures Imageworks 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the 00017 * distribution. Neither the name of Sony Pictures Imageworks nor the 00018 * names of its contributors may be used to endorse or promote 00019 * products derived from this software without specific prior written 00020 * permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00029 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00031 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00032 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00033 * OF THE POSSIBILITY OF SUCH DAMAGE. 00034 */ 00035 00036 //----------------------------------------------------------------------------// 00037 00038 #include <vector> 00039 00040 #include <boost/lexical_cast.hpp> 00041 #include <boost/timer.hpp> 00042 00043 #include <Field3D/SparseField.h> 00044 #include <Field3D/SparseFile.h> 00045 #include <Field3D/Field3DFile.h> 00046 #include <Field3D/FieldInterp.h> 00047 #include <Field3D/InitIO.h> 00048 #include <Field3D/Log.h> 00049 00050 //----------------------------------------------------------------------------// 00051 00052 using namespace boost; 00053 using namespace std; 00054 00055 using namespace Field3D; 00056 00057 //----------------------------------------------------------------------------// 00058 00060 template <class T> 00061 std::string str(const T& t) 00062 { 00063 return boost::lexical_cast<std::string>(t); 00064 } 00065 00066 //----------------------------------------------------------------------------// 00067 00068 int main(int argc, char **argv) 00069 { 00070 std::string filename("test_file.f3d"); 00071 std::string attribName("attrib"); 00072 00073 // Initialize IO 00074 initIO(); 00075 00076 int numFields = 5; 00077 00078 if (argc == 2) { 00079 try { 00080 numFields = lexical_cast<int>(argv[1]); 00081 } 00082 catch (boost::bad_lexical_cast &e) { 00083 Msg::print("Couldn't parse integer number. Aborting"); 00084 exit(1); 00085 } 00086 } else { 00087 // No voxel res given 00088 Msg::print("Usage: " + str(argv[0]) + 00089 " <num_fields>"); 00090 Msg::print("Got no number of fields. Using default."); 00091 } 00092 00093 // Create fields --- 00094 00095 Msg::print("Creating " + str(numFields) + " fields"); 00096 00097 SparseFieldf::Vec fields; 00098 00099 for (int i = 0; i < numFields; i++) { 00100 // Create 00101 SparseFieldf::Ptr field(new SparseFieldf); 00102 field->setSize(V3i(128)); 00103 field->name = str(i); 00104 field->attribute = attribName; 00105 // Fill with values 00106 SparseFieldf::iterator fi = field->begin(), fend = field->end(); 00107 for (; fi != fend; ++fi) { 00108 *fi = i; 00109 } 00110 fields.push_back(field); 00111 } 00112 00113 // Write fields --- 00114 00115 Msg::print("Writing fields"); 00116 00117 Field3DOutputFile out; 00118 out.create(filename); 00119 00120 for (int i = 0; i < numFields; i++) { 00121 out.writeScalarLayer<float>(fields[i]); 00122 } 00123 00124 // Read with dynamic loading --- 00125 00126 Msg::print("Reading fields"); 00127 00128 SparseFileManager::singleton().setLimitMemUse(true); 00129 00130 Field<float>::Vec loadedFields; 00131 00132 Field3DInputFile in; 00133 in.open(filename); 00134 00135 for (int i = 0; i < numFields; i++) { 00136 Field<float>::Vec fields = in.readScalarLayers<float>(str(i), attribName); 00137 if (fields.size() != 1) { 00138 Msg::print("Got the wrong # of fields. Aborting."); 00139 exit(1); 00140 } 00141 loadedFields.push_back(fields[0]); 00142 } 00143 00144 // Compare fields --- 00145 00146 Msg::print("Comparing fields"); 00147 00148 FIELD3D_RAND48 rng(10512); 00149 00150 Msg::print(" Mem use before access: "); 00151 00152 for (int i = 0; i < numFields; i++) { 00153 Msg::print(" Field " + str(i) + " : " + 00154 str(fields[i]->memSize())); 00155 Msg::print(" Loaded field " + str(i) + ": " + 00156 str(loadedFields[i]->memSize())); 00157 } 00158 00159 LinearFieldInterp<float> interp; 00160 for (int sample = 0; sample < 1000; sample++) { 00161 V3d lsP(rng.nextf(), rng.nextf(), rng.nextf()), vsP; 00162 for (int i = 0; i < numFields; i++) { 00163 fields[i]->mapping()->localToVoxel(lsP, vsP); 00164 float value = interp.sample(*fields[i], vsP); 00165 float loadedValue = interp.sample(*loadedFields[i], vsP); 00166 if (value != loadedValue) { 00167 Msg::print("Got a bad value at " + str(vsP)); 00168 exit(1); 00169 } 00170 } 00171 } 00172 00173 Msg::print(" Mem use after access: "); 00174 00175 for (int i = 0; i < numFields; i++) { 00176 Msg::print(" Field " + str(i) + " : " + 00177 str(fields[i]->memSize())); 00178 Msg::print(" Loaded field " + str(i) + ": " + 00179 str(loadedFields[i]->memSize())); 00180 } 00181 00182 } 00183 00184 //----------------------------------------------------------------------------//