| 1 | #include <stdio.h>
|
|---|
| 2 | #include <malloc.h>
|
|---|
| 3 |
|
|---|
| 4 | #define ESHP_INTERNAL
|
|---|
| 5 | #include "Shape.h"
|
|---|
| 6 |
|
|---|
| 7 | IndexedPointList::IndexedPointList()
|
|---|
| 8 | {
|
|---|
| 9 | mPartIndexes = NULL;
|
|---|
| 10 | mPoints = NULL;
|
|---|
| 11 | mNParts = 0;
|
|---|
| 12 | mNPoints = 0;
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | IndexedPointList::~IndexedPointList()
|
|---|
| 16 | {
|
|---|
| 17 | freeBuffers();
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | void IndexedPointList::allocBuffers()
|
|---|
| 21 | {
|
|---|
| 22 | freeBuffers();
|
|---|
| 23 |
|
|---|
| 24 | mPartIndexes = (ESInt32*)malloc(sizeof(ESInt32) * mNParts);
|
|---|
| 25 | mPoints = (ESPointEx*)malloc(sizeof(ESPointEx) * mNPoints);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | void IndexedPointList::freeBuffers()
|
|---|
| 29 | {
|
|---|
| 30 | if (mPartIndexes)
|
|---|
| 31 | {
|
|---|
| 32 | free(mPartIndexes);
|
|---|
| 33 | mPartIndexes = NULL;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | if (mPoints)
|
|---|
| 37 | {
|
|---|
| 38 | free(mPoints);
|
|---|
| 39 | mPoints = NULL;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void IndexedPointList::copyFrom(const IndexedPointList& aSrc)
|
|---|
| 44 | {
|
|---|
| 45 | mNParts = aSrc.mNParts;
|
|---|
| 46 | mNPoints = aSrc.mNPoints;
|
|---|
| 47 | allocBuffers();
|
|---|
| 48 |
|
|---|
| 49 | memcpy(mPoints , aSrc.mPoints , sizeof(ESPointEx) * mNPoints);
|
|---|
| 50 | memcpy(mPartIndexes, aSrc.mPartIndexes, sizeof(ESInt32 ) * mNParts);
|
|---|
| 51 | memcpy(mBounds , aSrc.mBounds , sizeof(ESDouble ) * 4);
|
|---|
| 52 |
|
|---|
| 53 | mZRange[0] = aSrc.mZRange[0];
|
|---|
| 54 | mZRange[1] = aSrc.mZRange[1];
|
|---|
| 55 |
|
|---|
| 56 | mMRange[0] = aSrc.mMRange[0];
|
|---|
| 57 | mMRange[1] = aSrc.mMRange[1];
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | es_error_t IndexedPointList::readFromMainFile(FILE* fp, ShpReadContext& rctx, int option)
|
|---|
| 61 | {
|
|---|
| 62 | es_error_t rv = ESHP_OK;
|
|---|
| 63 |
|
|---|
| 64 | mBounds[0] = Shape::freadLittleDouble(fp);
|
|---|
| 65 | mBounds[1] = Shape::freadLittleDouble(fp);
|
|---|
| 66 | mBounds[2] = Shape::freadLittleDouble(fp);
|
|---|
| 67 | mBounds[3] = Shape::freadLittleDouble(fp);
|
|---|
| 68 | rctx.readSize += 32;
|
|---|
| 69 | rctx.readRecordContentSize += 32;
|
|---|
| 70 |
|
|---|
| 71 | mNParts = Shape::freadLittle32(fp);
|
|---|
| 72 | mNPoints = Shape::freadLittle32(fp);
|
|---|
| 73 | rctx.readSize += 8;
|
|---|
| 74 | rctx.readRecordContentSize += 8;
|
|---|
| 75 |
|
|---|
| 76 | allocBuffers();
|
|---|
| 77 |
|
|---|
| 78 | ESInt32 i;
|
|---|
| 79 | for (i = 0;i < mNParts;i++)
|
|---|
| 80 | {
|
|---|
| 81 | mPartIndexes[i] = Shape::freadLittle32(fp);
|
|---|
| 82 | rctx.readSize += 4;
|
|---|
| 83 | rctx.readRecordContentSize += 4;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | for (i = 0;i < mNPoints;i++)
|
|---|
| 87 | {
|
|---|
| 88 | mPoints[i].x = Shape::freadLittleDouble(fp);
|
|---|
| 89 | mPoints[i].y = Shape::freadLittleDouble(fp);
|
|---|
| 90 | rctx.readSize += 16;
|
|---|
| 91 | rctx.readRecordContentSize += 16;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | if (option & SHPRECORD_WITH_Z)
|
|---|
| 95 | {
|
|---|
| 96 | // Z
|
|---|
| 97 | mZRange[0] = Shape::freadLittleDouble(fp);
|
|---|
| 98 | mZRange[1] = Shape::freadLittleDouble(fp);
|
|---|
| 99 | rctx.readSize += 16;
|
|---|
| 100 | rctx.readRecordContentSize += 16;
|
|---|
| 101 |
|
|---|
| 102 | for (i = 0;i < mNPoints;i++)
|
|---|
| 103 | {
|
|---|
| 104 | mPoints[i].z = Shape::freadLittleDouble(fp);
|
|---|
| 105 | rctx.readSize += 8;
|
|---|
| 106 | rctx.readRecordContentSize += 8;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | // Measure
|
|---|
| 110 | mMRange[0] = Shape::freadLittleDouble(fp);
|
|---|
| 111 | mMRange[1] = Shape::freadLittleDouble(fp);
|
|---|
| 112 | rctx.readSize += 16;
|
|---|
| 113 | rctx.readRecordContentSize += 16;
|
|---|
| 114 |
|
|---|
| 115 | for (i = 0;i < mNPoints;i++)
|
|---|
| 116 | {
|
|---|
| 117 | mPoints[i].m = Shape::freadLittleDouble(fp);
|
|---|
| 118 | rctx.readSize += 8;
|
|---|
| 119 | rctx.readRecordContentSize += 8;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | return rv;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | VALUE IndexedPointList::getPartAsRubyObj(int index)
|
|---|
| 127 | {
|
|---|
| 128 | if (index < 0 || index >= mNParts)
|
|---|
| 129 | return Qnil;
|
|---|
| 130 |
|
|---|
| 131 | int len = (index == (mNParts-1)) ? mNPoints : mPartIndexes[index+1];
|
|---|
| 132 | len -= mPartIndexes[index];
|
|---|
| 133 |
|
|---|
| 134 | int start = mPartIndexes[index];
|
|---|
| 135 |
|
|---|
| 136 | VALUE arr = rb_ary_new2(len);
|
|---|
| 137 | for (int i = 0;i < len;i++)
|
|---|
| 138 | {
|
|---|
| 139 | ESPointEx& p = mPoints[start+i];
|
|---|
| 140 | ShpPoint* shp = new ShpPoint(p.x, p.y, p.z, p.m);
|
|---|
| 141 | rb_ary_push(arr, shp->toRubyObject());
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | return arr;
|
|---|
| 145 | }
|
|---|