Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/volumetricMesh/interpolationWeightsMultiLoad.cpp

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "reducedStvk" library , Copyright (C) 2007 CMU, 2009 MIT *
6 * All rights reserved. *
7 * *
8 * Code author: Jernej Barbic *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 /*
30 Load/save many sets of interpolation weights from one file.
31 */
32
33 #include <stdio.h>
34 #include "interpolationWeightsMultiLoad.h"
35 #include "volumetricMesh.h"
36 #include "vegalong.h"
37
38 // input: filename, numModels
39 // output: numTargetLocations, numElementVertices, vertices, weights
40 int multiLoadInterpolationWeightsBinary(const char * filename, int * numModels, int ** numTargetLocations, int ** numElementVertices, int *** vertices, double *** weights) // binary version; returns 0 on success
41 {
42 FILE * fin = fopen(filename, "rb");
43 if (!fin)
44 {
45 printf("Error: unable to open file %s.\n", filename);
46 return 1;
47 }
48
49 int readItems = (int) fread(numModels, sizeof(int), 1, fin);
50 if (readItems < 1)
51 {
52 printf("Error: unable to read from file %s.\n", filename);
53 return 1;
54 }
55
56 (*numTargetLocations) = (int*) malloc (sizeof(int) * *numModels);
57 (*numElementVertices) = (int*) malloc (sizeof(int) * *numModels);
58 (*vertices) = (int**) malloc (sizeof(int*) * *numModels);
59 (*weights) = (double**) malloc (sizeof(double*) * *numModels);
60
61 vegaunsignedlong * header = (vegaunsignedlong*) malloc (sizeof(vegaunsignedlong) * *numModels); // we only read the header, but don't use it
62
63 readItems = (int) fread(header, sizeof(vegaunsignedlong), *numModels, fin);
64 if (readItems < *numModels)
65 {
66 printf("Error: unable to read from file %s.\n", filename);
67 return 1;
68 }
69 free(header);
70
71 int code = 0;
72 for(int i=0; i<*numModels; i++)
73 code = code | VolumetricMesh::loadInterpolationWeightsBinary(fin, &(*numTargetLocations)[i], &(*numElementVertices)[i], &(*vertices)[i], &(*weights)[i]);
74
75 fclose(fin);
76
77 return code;
78 }
79
80 // input: filename, numTargetLocations (array), numElementVertices (array), vertices, weights
81 int multiSaveInterpolationWeightsBinary(const char * filename, int numModels, int * numTargetLocations, int * numElementVertices, int ** vertices, double ** weights) // binary version; returns 0 on success
82 {
83 FILE * fout = fopen(filename, "wb");
84 if (!fout)
85 {
86 printf("Error: unable to open file %s.\n", filename);
87 return 1;
88 }
89
90 int writtenItems = (int)fwrite(&numModels, sizeof(int), 1, fout);
91 if (writtenItems < 1)
92 {
93 printf("Error: unable to write to file %s.\n", filename);
94 return 1;
95 }
96
97 vegaunsignedlong numBytes = 0;
98 for(int i=0; i<numModels; i++)
99 {
100 writtenItems = (int)fwrite(&numBytes, sizeof(vegaunsignedlong), 1, fout);
101 if (writtenItems < 1)
102 {
103 printf("Error: unable to write to file %s.\n", filename);
104 return 1;
105 }
106 numBytes += 2 * sizeof(int) + (vegaunsignedlong) numElementVertices[i] * numTargetLocations[i] * (sizeof(int) + sizeof(double));
107 }
108
109 int code = 0;
110 for(int i=0; i<numModels; i++)
111 code = code | VolumetricMesh::saveInterpolationWeightsBinary(fout, numTargetLocations[i], numElementVertices[i], vertices[i], weights[i]);
112
113 fclose(fout);
114 return code;
115 }
116