📄 teheightmap.cpp
字号:
void TeHeightMap::getStats(float *min, float *max, float *average){ if (min!=NULL || max!=NULL || average!=NULL) { if (flags.minUpdated && flags.maxUpdated && flags.averageUpdated) { if (min!=NULL) *min = minValue; if (max!=NULL) *max = maxValue; if (average!=NULL) *average = averageValue; } else { float sum = 0; minValue = 1000000; maxValue = -1000000; for (int i=0; i<resx*resy; i++) { sum += values[i]; if (values[i]<minValue) minValue = values[i]; if (values[i]>maxValue) maxValue = values[i]; } averageValue = sum/(resx*resy); flags.minUpdated = 1; flags.maxUpdated = 1; flags.averageUpdated = 1; if (min!=NULL) *min = minValue; if (max!=NULL) *max = maxValue; if (average!=NULL) *average = averageValue; } }}//----------------------------------------------------------------------------/** * Finds minimal value in entire heightmap. * * Uses TeHeightMap::flags for speedup. * * \return Minimal height from the heightmap values. * * \sa getStats(), minValue */float TeHeightMap::getMinValue(){ if (!flags.minUpdated) { minValue = 1000000; for (int i=0; i<resx*resy; i++) if (values[i]<minValue) minValue = values[i]; flags.minUpdated = 1; } return minValue;}//----------------------------------------------------------------------------/** * Finds maximal value in entire heightmap. * * Uses TeHeightMap::flags for speedup. * * \return Maximal height from the heightmap values. * * \sa getStats(), maxValue */float TeHeightMap::getMaxValue(){ if (!flags.maxUpdated) { maxValue = -1000000; for (int i=0; i<resx*resy; i++) if (values[i]>maxValue) maxValue = values[i]; flags.maxUpdated = 1; } return maxValue;}//----------------------------------------------------------------------------/** * Computes average value from entire heightmap. * * Uses TeHeightMap::flags for speedup. * * \return Average height from the heightmap values. * * \sa getStats(), averageValue */float TeHeightMap::getAverageValue(){ if (!flags.averageUpdated) { float sum = 0; for (int i=0; i<resx*resy; i++) sum += values[i]; averageValue = sum/(resx*resy); flags.averageUpdated = 1; } return averageValue;}//----------------------------------------------------------------------------/** * Adds \a delta to all values. * * \param delta This value will be added to all heightmap values. */void TeHeightMap::shift(const float delta){ if (delta != 0) { for (int i=0; i<resx*resy; i++) { values[i] += delta; } minValue += delta; maxValue += delta; averageValue += delta; // ?? OK ?? }}//----------------------------------------------------------------------------/** * Shifts heightmap values, \a value will become its minimal value. * * \param value Value that will become new minimal value in the heightmap. */void TeHeightMap::shiftMinValue(const float value){ shift(value - getMinValue());}//----------------------------------------------------------------------------/** * Shifts heightmap values, \a value will become its minimal value. * * We know \a old minimal value, so we do not have to compute it (it's faster, * no check). * * \param value Value that will become new minimal value in the heightmap. * \param old Old minimal value (no check). */void TeHeightMap::shiftMinValue(const float value, const float old){ shift(value - old);}//----------------------------------------------------------------------------/** * Shifts heightmap values, \a value will become its maximal value. * * \param value Value that will become new maximal value in the heightmap. */void TeHeightMap::shiftMaxValue(const float value){ shift(value - getMaxValue());}//----------------------------------------------------------------------------/** * Shifts heightmap values, \a value will become its maximal value. * * We know \a old maximal value, so we do not have to compute it (it's faster, * no check). * * \param value Value that will become new maximal value in the heightmap. * \param old Old maximal value (no check). */void TeHeightMap::shiftMaxValue(const float value, const float old){ shift(value - old);}//----------------------------------------------------------------------------/** * Shifts heightmap values, \a value will become its average value. * * \param value Value that will become new average value in the heightmap. */void TeHeightMap::shiftAverageValue(const float value){ shift(value - getAverageValue());}//----------------------------------------------------------------------------/** * Shifts heightmap values, \a value will become its average value. * * We know \a old average value, so we do not have to compute it (it's faster, * no check). * * \param value Value that will become new average value in the heightmap. * \param old Old average value (no check). */void TeHeightMap::shiftAverageValue(const float value, const float old){ shift(value - old);}//----------------------------------------------------------------------------/** * Copies all possible values from source heightmap. * * Does NOT CHANGE SIZE of the heightmap. If size of heightmaps differs, only * subset is copied. * * \param hmap Pointer to the source heightmap. */void TeHeightMap::copyFrom(const TeHeightMap *hmap){ if (resx==hmap->resx && resy==hmap->resy) this->operator=(*hmap); else { copyFrom(hmap, SbVec2s(0,0), getResolution()); resetFlags(); }}//----------------------------------------------------------------------------/** * Copies selected part of source heightmap. * * Copied area is specified by its top-left corner \a sorigin and \a size, * values are pasted at \a dorigin coordinates. * * \param hmap Pointer to the source heightmap. * \param sorigin Top-left corner of selected area in source heightmap. * \param size Size of selected area. * \param dorigin Top-left corner of area in target heightmap. */void TeHeightMap::copyFrom(const TeHeightMap *hmap, const SbVec2s &sorigin, const SbVec2s &size, const SbVec2s &dorigin){ if (sorigin[0]<hmap->resx && sorigin[1]<hmap->resy && resx>0 && resy>0) { // dimension-check in source int sizex = ((sorigin[0]+size[0]<=hmap->resx) ? size[0] : hmap->resx-sorigin[0] ); int sizey = ((sorigin[1]+size[1]<=hmap->resy) ? size[1] : hmap->resy-sorigin[1] ); // dimension-check in target if (sizex>this->resx-dorigin[0]) sizex = this->resx-dorigin[0]; if (sizey>this->resy-dorigin[1]) sizey = this->resy-dorigin[1]; if (sizex>0 && sizey>0) { // copying for (int i=0; i<sizey; i++) { memcpy( (void*)(values + ((dorigin[1]*this->resx)+(i*this->resx)+dorigin[0])), (const void*)(hmap->values + ((sorigin[1]*hmap->resx)+(i*hmap->resx)+sorigin[0])), sizex*sizeof(float) ); } resetFlags(); } }}//----------------------------------------------------------------------------/** * Two heightmaps all-values adding. * * Values from source heightmap will be added to "this" heightmap according * to a "blend-rate" \a rate. This should be greater than 0.0f. All source * values are multiplied by \a rate before adding to target values. * * All possible values will be added, both source and target top-left * corners are in (0,0) coordinates. * * \param hmap Pointer to the source heightmap. * \param rate Blend-rate showing how much of source heightmap to be added * to the target. */void TeHeightMap::addFrom(const TeHeightMap *hmap, const float rate){ addFrom(hmap, SbVec2s(0,0), getResolution(), SbVec2s(0,0), rate);}//----------------------------------------------------------------------------/** * Two heightmaps specified-values adding. * * Values from source heightmap will be added to "this" heightmap according * to a "blend-rate" \a rate. This should be greater than 0.0f. All source * values are multiplied by \a rate before adding to target values. * * Selected source area is specified by its top-left corner \a sorogin and its * \a size. Target area is specified by its top-left corner \a dorogin. * * \param hmap Pointer to the source heightmap. * \param sorigin Top-left corner of selected area in source heightmap. * \param size Size of selected area. * \param dorigin Top-left corner of area in target heightmap. * \param rate Blend-rate showing how much of source heightmap to be added * to the target. */void TeHeightMap::addFrom(const TeHeightMap *hmap, const SbVec2s &sorigin, const SbVec2s &size, const SbVec2s &dorigin, const float rate){ if (sorigin[0]<hmap->resx && sorigin[1]<hmap->resy && resx>0 && resy>0 && rate>0) { // dimension-check in source int sizex = ((sorigin[0]+size[0]<=hmap->resx) ? size[0] : hmap->resx-sorigin[0] ); int sizey = ((sorigin[1]+size[1]<=hmap->resy) ? size[1] : hmap->resy-sorigin[1] ); // dimension-check in target if (sizex>this->resx-dorigin[0]) sizex = this->resx-dorigin[0]; if (sizey>this->resy-dorigin[1]) sizey = this->resy-dorigin[1]; if (sizex>0 && sizey>0) { // adding for (int y=0; y<sizey; y++) { for (int x=0; x<sizex; x++) { values[(y*this->resx)+(dorigin[1]*this->resx)+dorigin[0]+x] += hmap->values[(y*hmap->resx)+(sorigin[1]*hmap->resx)+sorigin[0]+x]*rate; } } resetFlags(); } }}//----------------------------------------------------------------------------/** * Adds constant to all heightmap values. * * Same as shift(value). * * \param value Constant to be added to all heightmap values. * * \sa shift() */void TeHeightMap::addAll(const float value){ shift(value);}//----------------------------------------------------------------------------/** * Two heightmaps all-values subtracting. * * Values from source heightmap will be subtracted from "this" heightmap * according to a "blend-rate" \a rate. This should be greater than 0.0f. All * source values are multiplied by \a rate before subtracting from target * values. * * All possible values will be subtracted, both source and target top-left * corners are in (0,0) coordinates. * * \param hmap Pointer to the source heightmap. * \param rate Blend-rate showing how much of source heightmap to be * subtracted from the target. */void TeHeightMap::subtractFrom(const TeHeightMap *hmap, const float rate){ subtractFrom(hmap, SbVec2s(0,0), getResolution(), SbVec2s(0,0), rate);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -