| 1 | /* CubeGrid.cpp |
| 2 | * |
| 3 | * Copyright (C) 2023 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "CubeGrid.h" |
| 20 | #include "Graphics.h" |
| 21 | |
| 22 | #include "oo_DESTROY.h" |
| 23 | #include "CubeGrid_def.h" |
| 24 | #include "oo_COPY.h" |
| 25 | #include "CubeGrid_def.h" |
| 26 | #include "oo_EQUAL.h" |
| 27 | #include "CubeGrid_def.h" |
| 28 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 29 | #include "CubeGrid_def.h" |
| 30 | #include "oo_WRITE_TEXT.h" |
| 31 | #include "CubeGrid_def.h" |
| 32 | #include "oo_READ_TEXT.h" |
| 33 | #include "CubeGrid_def.h" |
| 34 | #include "oo_WRITE_BINARY.h" |
| 35 | #include "CubeGrid_def.h" |
| 36 | #include "oo_READ_BINARY.h" |
| 37 | #include "CubeGrid_def.h" |
| 38 | #include "oo_DESCRIPTION.h" |
| 39 | #include "CubeGrid_def.h" |
| 40 | |
| 41 | Thing_implement (CubePoint, Function, 0); |
| 42 | |
| 43 | Thing_implement (CubeTier, Function, 0); |
| 44 | |
| 45 | Thing_implement (CubeGrid, Function, 0); |
| 46 | |
| 47 | autoCubePoint CubeGrid_average (CubeGrid me, integer tierNumber, double tmin, double tmax) { |
| 48 | try { |
| 49 | Melder_require (tierNumber >= 1, U"Tier number should be positive"); |
| 50 | Melder_require (tierNumber <= my tiers.size, |
| 51 | U"Tier number (", tierNumber, U") should not be higher than the number of tiers (", my tiers.size, U")."); |
| 52 | CubeTier tier = my tiers.at [tierNumber]; |
| 53 | autoCubePoint you = Thing_new (CubePoint); |
| 54 | your red = 0.0; |
| 55 | your green = 0.0; |
| 56 | your blue = 0.0; |
| 57 | double numberOfSeconds = 0.0; |
| 58 | for (integer ipoint = 1; ipoint <= tier -> points.size; ipoint ++) { |
| 59 | CubePoint inpoint = tier -> points.at [ipoint]; |
| 60 | if (inpoint -> xmax > tmin && inpoint -> xmin < tmax) { |
| 61 | const double duration = inpoint -> xmax - inpoint -> xmin; |
| 62 | your red += inpoint -> red * duration; |
| 63 | your green += inpoint -> green * duration; |
| 64 | your blue += inpoint -> blue * duration; |
| 65 | numberOfSeconds += duration; |
| 66 | } |
| 67 | } |
| 68 | if (numberOfSeconds == 0.0) { |
| 69 | your red = undefined; |
| 70 | your green = undefined; |
| 71 | your blue = undefined; |
| 72 | } else { |
| 73 | your red /= numberOfSeconds; |
| 74 | your green /= numberOfSeconds; |
| 75 | your blue /= numberOfSeconds; |
| 76 | } |
| 77 | return you; |
| 78 | } catch (MelderError) { |
| 79 | Melder_throw (me, U": average not computed."); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | autoVEC CubeGrid_getAverages (CubeGrid me, integer tierNumber, double tmin, double tmax) { |
| 84 | autoCubePoint point = CubeGrid_average (me, tierNumber, tmin, tmax); |
| 85 | autoVEC result = raw_VEC (3); |
| 86 | result [1] = point -> red; |
| 87 | result [2] = point -> green; |
| 88 | result [3] = point -> blue; |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | void CubeGrid_paintInside (CubeGrid me, Graphics graphics, double tmin, double tmax) { |
| 93 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 94 | Graphics_setColour (graphics, Melder_WHITE); |
| 95 | Graphics_setWindow (graphics, 0.0, 1.0, 0.0, 1.0); |
| 96 | Graphics_fillRectangle (graphics, 0.0, 1.0, 0.0, 1.0); |
| 97 | Graphics_setWindow (graphics, tmin, tmax, 0.0, my tiers.size); |
| 98 | for (integer itier = 1; itier <= my tiers.size; itier ++) { |
| 99 | CubeTier tier = my tiers.at [itier]; |
| 100 | const double ymin = my tiers.size - itier, ymax = ymin + 1.0; |
| 101 | for (integer ipoint = 1; ipoint <= tier -> points.size; ipoint ++) { |
| 102 | CubePoint point = tier -> points.at [ipoint]; |
| 103 | if (point -> xmax > tmin && point -> xmin < tmax) { |
| 104 | const double xmin = Melder_clippedLeft (tmin, point -> xmin); |
| 105 | const double xmax = Melder_clippedRight (point -> xmax, tmax); |
| 106 | Graphics_setColour (graphics, MelderColour (point -> red, point -> green, point -> blue)); |
| 107 | Graphics_fillRectangle (graphics, xmin, xmax, ymin, ymax); |
| 108 | } |
| 109 | } |
| 110 | Graphics_setColour (graphics, Melder_BLACK); |
| 111 | if (itier > 1) { |
| 112 | Graphics_setLineWidth (graphics, 1.0); |
| 113 | Graphics_line (graphics, tmin, ymax, tmax, ymax); |
| 114 | } |
| 115 | } |
| 116 | Graphics_setLineWidth (graphics, 1.0); |
| 117 | Graphics_setColour (graphics, Melder_BLACK); |
| 118 | } |
| 119 | |
| 120 | void CubeGrid_paint (CubeGrid me, Graphics g, double tmin, double tmax, bool garnish) { |
| 121 | Graphics_setInner (g); |
| 122 | CubeGrid_paintInside (me, g, tmin, tmax); |
| 123 | Graphics_unsetInner (g); |
| 124 | if (garnish) { |
| 125 | Graphics_drawInnerBox (g); |
| 126 | Graphics_textBottom (g, true, U"Time (s)"); |
| 127 | Graphics_marksBottom (g, 2, true, true, false); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* End of file CubeGrid.cpp */ |
| 132 |