Skip to content

Commit

Permalink
Minor code optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
JustWhit3 committed Jul 25, 2023
1 parent 4887dd0 commit c50290a
Show file tree
Hide file tree
Showing 33 changed files with 1,692 additions and 1,580 deletions.
8 changes: 7 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
BasedOnStyle: Google
IndentWidth: 4
UseTab: Never
NamespaceIndentation: All
NamespaceIndentation: All
EmptyLineAfterAccessModifier: Always
IndentPPDirectives: None
AccessModifierOffset: 0
IndentAccessModifiers: true
CommentPragmas: '^$'
ColumnLimit: 120
131 changes: 62 additions & 69 deletions include/osmanip/graphics/canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* @file canvas.hpp
* @author Miguel MJ (miguelmjvg@gmail.com)
* @date 2022-06-06
* @copyright Copyright (c) 2022 Gianluca Bianco under the MIT license.
* @copyright Copyright (c) 2022 Gianluca Bianco
* under the MIT license.
*/

//====================================================
Expand All @@ -20,91 +21,83 @@
//====================================================

// STD headers
#include <stdint.h>

#include <cstdint>
#include <string>
#include <string_view>
#include <vector>

namespace osm {

//====================================================
// Enums
// FrameStyle
//====================================================

// FrameStyle
/**
* @brief Enum class used to define the frame style.
* @brief Enum class used to define the frame
* style.
*/
typedef enum { EMPTY = 0, ASCII = 1, BOX = 2 } FrameStyle;

//====================================================
// Classes
// Canvas class
//====================================================

// Canvas
/**
* @brief Instances of this class are used to draw in a limited 2D space.
* All the functions that modify the canvas do it internally; to make the
* changes effective in the console, you must use the refresh method.
* @brief Instances of this class are used to
* draw in a limited 2D space. All the
* functions that modify the canvas do it
* internally; to make the changes effective
* in the console, you must use the refresh
* method.
*
*/
class Canvas {
public:
//====================================================
// Constructors
//====================================================
explicit Canvas(uint32_t width, uint32_t height);

//====================================================
// Setters
//====================================================
void enableFrame(bool frame_enabled);
void setFrame(FrameStyle, std::string_view feat = "");
void setBackground(char c, std::string_view feat = "");
void setWidth(uint32_t width);
void setHeight(uint32_t height);

//====================================================
// Getters
//====================================================
char getBackground() const;
std::string getBackgroundFeat() const;
bool isFrameEnabled() const;
std::string getFrameFeat() const;
FrameStyle getFrameStyle() const;
uint32_t getWidth() const;
uint32_t getHeight() const;

//====================================================
// Other methods
//====================================================
void clear();
void put(uint32_t x, uint32_t y, char c, std::string_view feat = "");
void refresh();

private:
//====================================================
// Private attributes
//====================================================
bool frame_enabled_;
FrameStyle frame_style_;
std::string frame_feat_;
char bg_char_;
std::string bg_feat_;
std::vector<char> char_buffer_;
std::vector<std::string> feat_buffer_;
bool already_drawn_;

//====================================================
// Private methods
//====================================================
void resizeCanvas();

protected:
//====================================================
// Protected attributes
//====================================================
uint32_t width_, height_;
public:

// Constructors
explicit Canvas(uint32_t width, uint32_t height);

// Setters
void enableFrame(bool frame_enabled);
void setFrame(FrameStyle, std::string_view feat = "");
void setBackground(char c, std::string_view feat = "");
void setWidth(uint32_t width);
void setHeight(uint32_t height);

// Getters
char getBackground() const;
std::string getBackgroundFeat() const;
bool isFrameEnabled() const;
std::string getFrameFeat() const;
FrameStyle getFrameStyle() const;
uint32_t getWidth() const;
uint32_t getHeight() const;

// Methods
void clear();
void put(uint32_t x, uint32_t y, char c, std::string_view feat = "");
void refresh();

private:

// Members
bool frame_enabled_;
FrameStyle frame_style_;
std::string frame_feat_;
char bg_char_;
std::string bg_feat_;
std::vector<char> char_buffer_;
std::vector<std::string> feat_buffer_;
bool already_drawn_;

// Constants
static const std::vector<std::vector<std::string>> frames;

// Methods
void resizeCanvas();

protected:

// Members
uint32_t width_, height_;
};
} // namespace osm

Expand Down
95 changes: 44 additions & 51 deletions include/osmanip/graphics/plot_2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@
#include <osmanip/graphics/canvas.hpp>

// STD headers
#include <stdint.h>

#include <cmath>
#include <cstdint>
#include <functional>
#include <string>

namespace osm {

//====================================================
// Classes
// Plot2DCanvas
//====================================================

// Plot2DCanvas
/**
* @brief This class is used to plot mathematical functions R -> R. In
* addition to all the properties of a basic Canvas, this one also has an
Expand All @@ -47,58 +45,53 @@ namespace osm {
*
*/
class Plot2DCanvas : public Canvas {
public:
//====================================================
// Constructors
//====================================================
explicit Plot2DCanvas(uint32_t w, uint32_t h);
public:

//====================================================
// Setters
//====================================================
void setOffset(float xOff, float yOff);
void setScale(float xScale, float yScale);
// Constructors
explicit Plot2DCanvas(uint32_t w, uint32_t h);

//====================================================
// Getters
//====================================================
float getOffsetX() const;
float getOffsetY() const;
float getScaleX() const;
float getScaleY() const;
// Setters
void setOffset(float xOff, float yOff);
void setScale(float xScale, float yScale);

//====================================================
// Methods
//====================================================
// Getters
float getOffsetX() const;
float getOffsetY() const;
float getScaleX() const;
float getScaleY() const;

// Draw
/**
* @brief Plot a function that receives an argument of a numeric type X
* and returns a numeric value of type Y. Represent it with a given char
* c and an optional feat.
*
* @tparam Y Type-argument of the given function.
* @tparam X Type-return of the given function.
* @param function The input function.
* @param c The char to represent a function.
* @param feat The optional feature.
*/
template <typename Y, typename X>
inline void draw(std::function<Y(X)> function, char c,
std::string_view feat = "") {
for (uint32_t x = 0; x < width_; x++) {
float real_x = offset_x_ + x * scale_x_;
Y real_y = function(real_x);
uint32_t y = std::round((real_y - offset_y_) / scale_y_);
if (y > 0 && y < height_) put(x, y, c, feat);
// Draw
/**
* @brief Plot a function that receives an argument of a numeric
* type X and returns a numeric value of type Y. Represent it with a
* given char c and an optional feat.
*
* @tparam Y Type-argument of the given function.
* @tparam X Type-return of the given function.
* @param function The input function.
* @param c The char to represent a function.
* @param feat The optional feature.
*/
template <typename Y, typename X>
inline void draw(std::function<Y(X)> function, char c, std::string_view feat = "") { //

// Find correct boundaries for the loop
uint32_t x_min = std::max(0, static_cast<int>(std::floor((0 - offset_x_) / scale_x_)));
uint32_t x_max = std::min(width_, static_cast<uint32_t>(std::ceil((width_ - offset_x_) / scale_x_)));

// Loop over the computed boundaries and draw
for (uint32_t x = x_min; x < x_max; x++) {
float real_x = offset_x_ + x * scale_x_;
Y real_y = function(real_x);
uint32_t y = static_cast<uint32_t>((real_y - offset_y_) / scale_y_);
if (y > 0 && y < height_) put(x, y, c, feat);
}
}
}

private:
//====================================================
// Private attributes
//====================================================
float offset_x_, offset_y_, scale_x_, scale_y_;
private:

// Members
float offset_x_, offset_y_, scale_x_, scale_y_;
};
} // namespace osm

Expand Down
7 changes: 5 additions & 2 deletions include/osmanip/manipulators/colsty.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//====================================================
/**
* @file colsty.hpp
* @author Gianluca Bianco (biancogianluca9@gmail.com)
* @author Gianluca Bianco
* (biancogianluca9@gmail.com)
* @date 2022-06-06
* @copyright Copyright (c) 2022 Gianluca Bianco under the MIT license.
* @copyright Copyright (c) 2022 Gianluca Bianco
* under the MIT license.
*/

//====================================================
Expand All @@ -29,6 +31,7 @@
#include <unordered_map>

namespace osm {

//====================================================
// Variables
//====================================================
Expand Down
12 changes: 7 additions & 5 deletions include/osmanip/manipulators/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//====================================================
/**
* @file common.hpp
* @author Gianluca Bianco (biancogianluca9@gmail.com)
* @author Gianluca Bianco
* (biancogianluca9@gmail.com)
* @date 2022-06-06
* @copyright Copyright (c) 2022 Gianluca Bianco under the MIT license.
* @copyright Copyright (c) 2022 Gianluca Bianco
* under the MIT license.
*/

//====================================================
Expand All @@ -24,12 +26,12 @@
#include <unordered_map>

namespace osm {

//====================================================
// Functions
//====================================================
extern const std::string& feat(
const std::unordered_map<std::string, std::string>& generic_map,
const std::string& feat_string);
extern const std::string &feat(const std::unordered_map<std::string, std::string> &generic_map,
const std::string &feat_string);
} // namespace osm

#endif
14 changes: 7 additions & 7 deletions include/osmanip/manipulators/cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//====================================================
/**
* @file cursor.hpp
* @author Gianluca Bianco (biancogianluca9@gmail.com)
* @author Gianluca Bianco
* (biancogianluca9@gmail.com)
* @date 2022-06-06
* @copyright Copyright (c) 2022 Gianluca Bianco under the MIT license.
* @copyright Copyright (c) 2022 Gianluca Bianco
* under the MIT license.
*/

//====================================================
Expand All @@ -30,11 +32,11 @@
#include <utility>

namespace osm {

//====================================================
// Aliases
//====================================================
using string_pair_map =
std::unordered_map<std::string, std::pair<std::string, std::string>>;
using string_pair_map = std::unordered_map<std::string, std::pair<std::string, std::string>>;

//====================================================
// Variables
Expand All @@ -45,9 +47,7 @@ namespace osm {
//====================================================
// Functions
//====================================================
extern const std::string feat(const string_pair_map& generic_map,
const std::string& feat_string,
int32_t feat_int);
extern const std::string feat(const string_pair_map &generic_map, const std::string &feat_string, int32_t feat_int);
extern const std::string go_to(int32_t x, int32_t y);
} // namespace osm

Expand Down
Loading

0 comments on commit c50290a

Please sign in to comment.