Skip to content

Commit

Permalink
rect: Use more efficient float calculations for ceil, floor
Browse files Browse the repository at this point in the history
This fixes warnings from LGTM:

Multiplication result may overflow 'float' before it is converted
to 'double'.

While the floor function always calculates with double, here the
overloaded std::floor can be used to handle the float arguments
more efficiently.

Replace also old C++ type casts by static_cast.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Oct 6, 2018
1 parent 1e4768c commit f264464
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/ccstruct/rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define RECT_H

#include <algorithm> // for std::max, std::min
#include <cmath> // for ceil, floor
#include <cmath> // for std::ceil, std::floor
#include <cstdint> // for INT16_MAX
#include <cstdio> // for FILE
#include "platform.h" // for DLLSYM
Expand Down Expand Up @@ -162,29 +162,33 @@ class DLLSYM TBOX { // bounding box

void move( // move box
const FCOORD vec) { // by float vector
bot_left.set_x ((int16_t) floor (bot_left.x () + vec.x ()));
bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() + vec.x())));
// round left
bot_left.set_y ((int16_t) floor (bot_left.y () + vec.y ()));
bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() + vec.y())));
// round down
top_right.set_x ((int16_t) ceil (top_right.x () + vec.x ()));
top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() + vec.x())));
// round right
top_right.set_y ((int16_t) ceil (top_right.y () + vec.y ()));
top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() + vec.y())));
// round up
}

void scale( // scale box
const float f) { // by multiplier
bot_left.set_x ((int16_t) floor (bot_left.x () * f)); // round left
bot_left.set_y ((int16_t) floor (bot_left.y () * f)); // round down
top_right.set_x ((int16_t) ceil (top_right.x () * f)); // round right
top_right.set_y ((int16_t) ceil (top_right.y () * f)); // round up
// round left
bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() * f)));
// round down
bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() * f)));
// round right
top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() * f)));
// round up
top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() * f)));
}
void scale( // scale box
const FCOORD vec) { // by float vector
bot_left.set_x ((int16_t) floor (bot_left.x () * vec.x ()));
bot_left.set_y ((int16_t) floor (bot_left.y () * vec.y ()));
top_right.set_x ((int16_t) ceil (top_right.x () * vec.x ()));
top_right.set_y ((int16_t) ceil (top_right.y () * vec.y ()));
bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() * vec.x())));
bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() * vec.y())));
top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() * vec.x())));
top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() * vec.y())));
}

// rotate doesn't enlarge the box - it just rotates the bottom-left
Expand Down Expand Up @@ -314,8 +318,10 @@ class DLLSYM TBOX { // bounding box
inline TBOX::TBOX( // constructor
const FCOORD pt // floating centre
) {
bot_left = ICOORD ((int16_t) floor (pt.x ()), (int16_t) floor (pt.y ()));
top_right = ICOORD ((int16_t) ceil (pt.x ()), (int16_t) ceil (pt.y ()));
bot_left = ICOORD(static_cast<int16_t>(std::floor(pt.x())),
static_cast<int16_t>(std::floor(pt.y())));
top_right = ICOORD(static_cast<int16_t>(std::ceil(pt.x())),
static_cast<int16_t>(std::ceil(pt.y())));
}


Expand Down

0 comments on commit f264464

Please sign in to comment.