source: trunk/src/mesh/element.cc @ 232

Last change on this file since 232 was 219, checked in by cxsong, 18 years ago

moved to src/mesh dir

File size: 3.4 KB
Line 
1#include "element.h"
2
3// default constructor
4RpElement::RpElement()
5{
6        m_nodes.resize(8);
7}
8
9// constructor
10// Input:
11//      number of nodes in element
12//      element id
13//
14RpElement::RpElement(int numNodes, int id)
15{
16        m_id = id;
17        if (m_nodes.capacity() != (unsigned)numNodes)
18                m_nodes.resize(numNodes);
19}
20
21//
22// constructor
23// Instantiate from a byte stream
24// id(int), numNodes(int), list of ids(int *)
25//
26RpElement::RpElement(const char* buf)
27{
28        deserialize(buf);
29}
30
31// add node name
32void RpElement::addNode(int nodeName)
33{
34        m_nodes.push_back(nodeName);
35}
36
37// add all the nodes to element
38RP_ERROR RpElement::addNodes(const int* nodes, int numNodes)
39{
40        // expand array if not big enough
41        if (m_nodes.capacity() < (unsigned)numNodes)
42                m_nodes.resize(numNodes);
43
44        for (int i=0; i < numNodes; i++) {
45                m_nodes[i] = nodes[i];
46        }
47
48        return RP_SUCCESS;
49}
50
51//
52// Return a list of node IDs
53// Input:
54//      inlist: int array
55//      len:    length of inlist
56// Output:
57//      inlist: IDs of nodes in this element
58//      len: number of nodes in element
59//
60RP_ERROR RpElement::getNodeIdList(int* inlist, int& len)
61{
62        int size = m_nodes.size();
63
64        if (inlist == NULL || len < size) {
65                RpAppendErr("RpElement::getNodeIdList: invalid array");
66                RpPrintErr();
67                return RP_ERR_INVALID_ARRAY;
68        }
69
70        for (int i=0; i < size; i++) {
71                inlist[i] = m_nodes[i];
72        }
73
74        len = size;
75
76        return RP_SUCCESS;
77}
78
79//
80// Serialization of RpElement:
81//      element id
82//      number of nodes in element
83//      node 1
84//      node 2
85//      ...
86//
87char*
88RpElement::serialize()
89{
90        int nbytes = this->numBytes() + 2*sizeof(int);
91
92        char * buf = new char[nbytes];
93        serialize(buf, nbytes);
94        return buf;
95}
96
97RP_ERROR
98RpElement::serialize(char* buf, int buflen)
99{
100        int nbytes = this->numBytes();
101
102        if (buf == NULL || buflen < (signed)(nbytes+2*sizeof(int))) {
103                RpAppendErr("RpNode::serialize: invalid buffer");
104                RpPrintErr();
105                return RP_ERR_INVALID_ARRAY;
106        }
107
108        char * ptr = buf;
109
110        // copy element id
111        memcpy((void *)ptr, (void *)&m_id, sizeof(int));
112        ptr += sizeof(int);
113
114        // copy number of nodes/element
115        int numNodes = m_nodes.size();
116        memcpy((void *)ptr, (void *)&numNodes, sizeof(int));
117        ptr += sizeof(int);
118
119        // copy all nodes
120        memcpy((void *)ptr, (void *)&(m_nodes[0]), numNodes*sizeof(int));
121
122        return RP_SUCCESS;
123}
124
125RP_ERROR
126RpElement::deserialize(const char* buf)
127{
128        if (buf == NULL) {
129                RpAppendErr("RpElement::deserialize: null buffer");
130                RpPrintErr();
131                return RP_ERR_NULL_PTR;
132        }
133
134        char * ptr = (char*)buf;
135
136        // copy element id
137        memcpy((void *)ptr, (void *)&m_id, sizeof(int));
138        ptr += sizeof(int);
139
140        // copy number of nodes
141        int numNodes;
142        memcpy((void *)ptr, (void *)&numNodes, sizeof(int));
143
144        if (m_nodes.capacity() != (unsigned)numNodes)
145                m_nodes.resize(numNodes);
146
147        for (int i=0; i < numNodes; i++) {
148                memcpy((void *)&(m_nodes[i]), (void *)ptr, sizeof(int));
149                ptr += sizeof(int);
150        }
151        return RP_SUCCESS;
152}
153
154void
155RpElement::xmlString(std::string& textString)
156{
157        // temp char string
158        char str[256];
159
160        // clear the input string
161        textString.erase();
162
163        sprintf(str, "<element id=\"%d\">\n\t<nodes>", m_id);
164        textString.append(str);
165
166        for (int i=0; i < (signed)(m_nodes.size()); i++) {
167                sprintf(str, "%d ", m_nodes[i]);
168                textString.append(str);
169        }
170        textString.append("</nodes>\n</element>");
171}
172
173void
174RpElement::print()
175{
176        printf("<element id=\"%d\">\n", m_id);
177        printf("\t<nodes>");
178        for (int i=0; i < (signed)(m_nodes.size()); i++)
179                printf("%d ", m_nodes[i]);
180        printf("</node>\n</element>");
181}
182
Note: See TracBrowser for help on using the repository browser.