├── LICENSE ├── README.md ├── imgs ├── 01.png ├── 02.png └── cla-11-02-cla-c-certified-associate-programmer.png ├── rastToVec.c ├── rastToVec.h └── tests └── line_patt ├── Makefile ├── checkLinePatt.c ├── checkLinePatt.h ├── main.c ├── rastToVec.c └── rastToVec.h /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # raster-to-vector 2 | A fast way to convert rasterized straight edges into vectors. 3 | 4 | ![](./imgs/01.png) ![](./imgs/02.png) 5 | 6 | If given the pixel pattern data of a line bit by bit, it is possible to determine if a line is straight without using loops or slow calculus functions. 7 | 8 | This is implemented in the "rastToVec.c" source code. 9 | 10 | In the "tests/line_patt" folder the output of the fast method is tested using the output of a different and more slower method. 11 | 12 | Objective: to create code that... 13 | * quickly converts edges into vectors 14 | * is practical and easy to use 15 | * uses a single c source code file 16 | 17 | Applications: 18 | * landscaping 19 | * home decorating 20 | * CAD 21 | * augmented reality 22 | * real time 3d modeling of the real world 23 | * camera based collision detection systems 24 | 25 | Todo List: 26 | * Write edge tracer code 27 | * Demonstrate raster to vector conversion 28 | -------------------------------------------------------------------------------- /imgs/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lowlevel86/raster-to-vector/b7d59fa3f50784281e064ae5a12798fe4d618beb/imgs/01.png -------------------------------------------------------------------------------- /imgs/02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lowlevel86/raster-to-vector/b7d59fa3f50784281e064ae5a12798fe4d618beb/imgs/02.png -------------------------------------------------------------------------------- /imgs/cla-11-02-cla-c-certified-associate-programmer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lowlevel86/raster-to-vector/b7d59fa3f50784281e064ae5a12798fe4d618beb/imgs/cla-11-02-cla-c-certified-associate-programmer.png -------------------------------------------------------------------------------- /rastToVec.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Crailin Mogged 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include "rastToVec.h" 20 | 21 | 22 | struct pattern_variables *iniPattVars(char *buff) 23 | { 24 | static struct pattern_variables pv; 25 | 26 | pv.buff = buff; 27 | pv.buffInc = 0; 28 | pv.ptrA = 0; 29 | pv.ptrB = 0; 30 | pv.cntA = 0; 31 | pv.cntB = 0; 32 | pv.incA = 0; 33 | pv.incB = 0; 34 | pv.multInc = 0; 35 | pv.multPrior = 0; 36 | pv.multH1 = 0; 37 | pv.multH2 = 0; 38 | pv.multA = 0; 39 | pv.multB = 0; 40 | pv.multOffset = 0; 41 | pv.identiSegInc = 0; 42 | pv.flipFlopSegInc = 0; 43 | pv.checkVal = 2; // 2 == any bit value; -1 == none 44 | 45 | return &pv; 46 | } 47 | 48 | 49 | void recalcBitPattVars(struct pattern_variables *pv) 50 | { 51 | int bitCntA, bitCntB; 52 | 53 | bitCntA = pv->multA * pv->cntA + pv->cntB; 54 | bitCntB = pv->multB * pv->cntA + pv->cntB; 55 | pv->ptrA = pv->buffInc+1 - (bitCntA + bitCntB); 56 | pv->ptrB = pv->buffInc+1 - bitCntB; 57 | pv->cntA = bitCntA; 58 | pv->cntB = bitCntB; 59 | } 60 | 61 | 62 | void accOffset(struct pattern_variables *pv) // account offset 63 | { 64 | if (pv->flipFlopSegInc == 0) 65 | { 66 | pv->multOffset = pv->identiSegInc + 1; 67 | pv->multInc = 0; 68 | pv->multH1 = 0; 69 | pv->multH2 = 0; 70 | pv->flipFlopSegInc = 0; 71 | } 72 | else if (pv->flipFlopSegInc == 1) 73 | { 74 | if (pv->multOffset) 75 | pv->multOffset = 0; 76 | else 77 | pv->multOffset = 1; 78 | 79 | pv->multInc = pv->identiSegInc + 1; 80 | pv->multH1 = pv->multInc; 81 | pv->multH2 = 0; 82 | pv->flipFlopSegInc = 0; 83 | } 84 | else if (pv->flipFlopSegInc == 2) 85 | { 86 | if (pv->multOffset) 87 | pv->multOffset = 1; 88 | else 89 | pv->multOffset = 0; 90 | 91 | pv->multInc = 2; 92 | pv->multH1 = 1; 93 | pv->multH2 = 2; 94 | pv->flipFlopSegInc = 1; 95 | } 96 | else if (pv->flipFlopSegInc > 2) 97 | { 98 | pv->multA = 1; 99 | pv->multB = 2; 100 | recalcBitPattVars(pv); 101 | pv->multOffset = pv->flipFlopSegInc - 1; 102 | pv->multInc = 0; 103 | pv->multH1 = 0; 104 | pv->multH2 = 0; 105 | pv->flipFlopSegInc = 0; 106 | } 107 | 108 | pv->multA = 0; 109 | pv->multB = 0; 110 | pv->identiSegInc = 0; 111 | } 112 | 113 | 114 | void recalcPattVars(struct pattern_variables *pv) 115 | { 116 | // account offsets that represent current largest possible 117 | if (pv->multInc == pv->multOffset-1) 118 | { 119 | if (pv->multH1 == 0) 120 | { 121 | pv->multH1 = pv->multOffset; 122 | pv->multOffset = 0; 123 | } 124 | else if (pv->multH2 == 0) 125 | { 126 | pv->multH1 = pv->multOffset; 127 | pv->multH2 = pv->multInc; 128 | pv->multA = pv->multOffset; 129 | pv->identiSegInc += 1; 130 | } 131 | } 132 | 133 | // account multiplier holders 134 | if (pv->multH1 == 0) 135 | pv->multH1 = pv->multInc; 136 | else if (pv->multH2 == 0) 137 | { 138 | if (pv->multInc != pv->multH1) 139 | pv->multH2 = pv->multInc; 140 | } 141 | 142 | // account both multA/B 143 | if (pv->multA == 0) 144 | { 145 | if (pv->multInc == pv->multPrior) 146 | pv->multA = pv->multInc; 147 | } 148 | else 149 | { 150 | if (pv->multInc != pv->multA) 151 | { 152 | pv->multB = pv->multInc; 153 | recalcBitPattVars(pv); 154 | } 155 | } 156 | 157 | if (pv->multB) 158 | accOffset(pv); 159 | else 160 | { 161 | // keep track of identical segments 162 | if (pv->multInc == pv->multPrior) 163 | pv->identiSegInc += 1; 164 | 165 | // keep track of the flip-flop pattern size 166 | if ((pv->multInc == pv->multH2) && (pv->multInc != pv->multPrior)) 167 | pv->flipFlopSegInc += 1; 168 | } 169 | } 170 | 171 | 172 | void growPattModel(int bitVal, struct pattern_variables *pv) 173 | { 174 | // find if the pattern matches 'A' or 'B' 175 | if (bitVal == pv->buff[pv->ptrA+pv->incA]) 176 | { 177 | if (pv->incA != -1) 178 | pv->incA += 1; 179 | } 180 | else 181 | pv->incA = -1; // does not match 182 | 183 | if (bitVal == pv->buff[pv->ptrB+pv->incB]) 184 | { 185 | if (pv->incB != -1) 186 | pv->incB += 1; 187 | } 188 | else 189 | pv->incB = -1; // does not match 190 | 191 | if (pv->incA == pv->cntA) // if the pattern matches 'A' 192 | { 193 | pv->multInc += 1; 194 | pv->incA = 0; 195 | pv->incB = 0; 196 | } 197 | 198 | if (pv->incB == pv->cntB) // if the pattern matches 'B' 199 | { 200 | recalcPattVars(pv); 201 | pv->multPrior = pv->multInc; 202 | pv->multInc = 0; 203 | pv->incA = 0; 204 | pv->incB = 0; 205 | } 206 | 207 | pv->buff[pv->buffInc] = bitVal; 208 | pv->buffInc += 1; 209 | } 210 | 211 | 212 | void resolveDuoBitVal(struct pattern_variables *pv) 213 | { 214 | if ((pv->incA != -1) && (pv->incB != -1)) 215 | { 216 | if (pv->buff[pv->ptrA+pv->incA] == pv->buff[pv->ptrB+pv->incB]) 217 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 218 | else 219 | pv->checkVal = 2; 220 | } 221 | else if (pv->incA != -1) 222 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 223 | else if (pv->incB != -1) 224 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 225 | } 226 | 227 | void duoPattCheck(struct pattern_variables *pv, int smMult, int lgMult) 228 | { 229 | if (pv->multInc == smMult) 230 | resolveDuoBitVal(pv); 231 | else if (pv->multInc == lgMult) 232 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 233 | else if (pv->multInc < smMult) 234 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 235 | } 236 | 237 | void trioPattCheck(struct pattern_variables *pv, int smMult, int medMult, int lgMult) 238 | { 239 | if (pv->multInc == smMult) 240 | resolveDuoBitVal(pv); 241 | else if (pv->multInc == medMult) 242 | resolveDuoBitVal(pv); 243 | else if (pv->multInc == lgMult) 244 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 245 | else if (pv->multInc < smMult) 246 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 247 | } 248 | 249 | void flipFlopPattCheck(struct pattern_variables *pv, int mult1, int mult2) 250 | { 251 | if (pv->multA == mult1) 252 | { 253 | if (pv->multInc == mult2) 254 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 255 | else if (pv->multInc < mult2) 256 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 257 | } 258 | else if (pv->multA == mult2) 259 | { 260 | if (pv->multInc == mult1) 261 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 262 | else if (pv->multInc < mult1) 263 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 264 | } 265 | } 266 | 267 | int checkBit(int bitVal, struct pattern_variables *pv) 268 | { 269 | pv->checkVal = -1; 270 | 271 | if ((pv->incA == -1) && (pv->incB == -1)) 272 | return 1; 273 | 274 | if ((pv->flipFlopSegInc >= 2) && (pv->multA)) // end of flip-flop pattern 275 | { 276 | flipFlopPattCheck(pv, pv->multH1, pv->multH2); 277 | } 278 | else if (pv->multH2) // check for 2 possible patterns 279 | { 280 | if (pv->multH1 == pv->multH2-1) // multH1 < multH2 281 | duoPattCheck(pv, pv->multH1, pv->multH2); 282 | else if (pv->multH2 == pv->multH1-1) // multH2 < multH1 283 | duoPattCheck(pv, pv->multH2, pv->multH1); 284 | } 285 | else if ((pv->multH1) && (pv->multH2 == 0)) // check for 3 possible patterns 286 | { 287 | trioPattCheck(pv, pv->multH1-1, pv->multH1, pv->multH1+1); 288 | } 289 | else if (pv->multH1 == 0) // pattern size is unknown 290 | { 291 | if (pv->multInc >= pv->multOffset-1) 292 | resolveDuoBitVal(pv); 293 | else if (pv->multInc < pv->multOffset-1) 294 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 295 | } 296 | 297 | if (bitVal == pv->checkVal) 298 | return 0; 299 | 300 | if (pv->checkVal == 2) 301 | return 0; 302 | 303 | return 1; 304 | } 305 | 306 | 307 | void iniGrowPattModel(int bitVal, struct pattern_variables *pv) 308 | { 309 | // find the bit value that occurs more often 310 | if (pv->cntA == 0) 311 | { 312 | if ((pv->buffInc) && (bitVal == pv->buff[pv->buffInc-1])) 313 | { 314 | pv->ptrA = pv->buffInc-1; 315 | pv->cntA = 1; 316 | pv->flipFlopSegInc = pv->buffInc >> 1; 317 | } 318 | } 319 | else if (pv->cntB == 0) // find the other bit 320 | { 321 | if (bitVal != pv->buff[pv->ptrA]) 322 | { 323 | pv->ptrB = pv->buffInc; 324 | pv->cntB = 1; 325 | 326 | // find offset 327 | if (pv->flipFlopSegInc == 0) // 00...1 328 | { 329 | pv->multOffset = pv->buffInc; 330 | } 331 | else if (pv->flipFlopSegInc == 1) // 0100...1 332 | { 333 | if (pv->buff[0] == pv->buff[pv->ptrA]) // 0100...1 else 100...1 334 | pv->multOffset = 1; 335 | 336 | pv->multPrior = pv->ptrB - pv->ptrA; 337 | pv->multH1 = pv->multPrior; 338 | pv->flipFlopSegInc = 0; 339 | } 340 | else if (pv->flipFlopSegInc == 2) // if flip-flop Pattern 0101001 341 | { 342 | if (pv->buff[0] == pv->buff[pv->ptrA]) // 0101001 else 101001 343 | pv->multOffset = 1; 344 | 345 | pv->multPrior = 2; 346 | pv->multH1 = 1; 347 | pv->multH2 = 2; 348 | pv->flipFlopSegInc = 1; 349 | } 350 | else if (pv->flipFlopSegInc > 2) // if flip-flop Pattern 010101...001 351 | { 352 | pv->ptrA -= 2; pv->ptrB -= 2; 353 | pv->cntA = 2; pv->cntB = 3; 354 | pv->multOffset = pv->flipFlopSegInc - 1; 355 | pv->flipFlopSegInc = 0; 356 | } 357 | } 358 | } 359 | 360 | pv->buff[pv->buffInc] = bitVal; 361 | pv->buffInc += 1; 362 | } 363 | 364 | int iniCheckBit(int bitVal, struct pattern_variables *pv) 365 | { 366 | pv->checkVal = 2; 367 | 368 | // find the possible bit values for flip-flop patterns 369 | if (pv->flipFlopSegInc >= 2) 370 | { 371 | if (pv->buff[pv->buffInc-2] == pv->buff[pv->ptrA]) 372 | pv->checkVal = !pv->buff[pv->ptrA]; 373 | 374 | if (pv->buff[pv->buffInc-3] == pv->buff[pv->ptrA]) 375 | pv->checkVal = -1; 376 | } 377 | 378 | if (bitVal == pv->checkVal) 379 | return 0; 380 | 381 | if (pv->checkVal == 2) 382 | return 0; 383 | 384 | return 1; 385 | } 386 | 387 | 388 | int checkLinePatt(int bitVal, struct pattern_variables *pv) 389 | { 390 | if (pv->cntB) 391 | { 392 | // check if the given pattern bit value is allowed 393 | if (checkBit(bitVal, pv)) 394 | return 1; 395 | 396 | // grow the pattern model that will be used to tell if the line is straight 397 | growPattModel(bitVal, pv); 398 | } 399 | else 400 | { 401 | if (iniCheckBit(bitVal, pv)) 402 | return 1; 403 | 404 | iniGrowPattModel(bitVal, pv); 405 | } 406 | 407 | return 0; 408 | } 409 | -------------------------------------------------------------------------------- /rastToVec.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Crailin Mogged 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | struct pattern_variables 18 | { 19 | char *buff; 20 | int buffInc; 21 | int ptrA; // 'A' occurs more often than 'B' 22 | int ptrB; 23 | int cntA; 24 | int cntB; 25 | int incA; 26 | int incB; 27 | int multInc; // if incA == cntA then multInc++ 28 | int multPrior; 29 | int multH1; // holds multA/B until they can be distinguished 30 | int multH2; 31 | int multA; // occurs more often than multB 32 | int multB; 33 | int multOffset; 34 | int identiSegInc; // identical segment increment 35 | int flipFlopSegInc; 36 | int checkVal; 37 | }; 38 | 39 | // iniPattVars() requires a pointer to a buffer with 40 | // the size of the window width for vertical edges 41 | // or window height for horizontal edges 42 | struct pattern_variables *iniPattVars(char *); 43 | 44 | int checkLinePatt(int, struct pattern_variables *); 45 | -------------------------------------------------------------------------------- /tests/line_patt/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | RES = 3 | OBJ = main.o rastToVec.o checkLinePatt.o$(RES) 4 | LIBS = -lm -pthread 5 | BIN = prog 6 | CFLAGS = -Wall 7 | RM = rm -f 8 | 9 | .PHONY: all all-before all-after clean clean-custom 10 | 11 | all: all-before $(BIN) all-after 12 | 13 | clean: clean-custom 14 | ${RM} $(OBJ) $(BIN) 15 | 16 | $(BIN): $(OBJ) 17 | $(CC) $(OBJ) -o $(BIN) $(LIBS) 18 | 19 | main.o: main.c 20 | $(CC) -c main.c -o main.o $(CFLAGS) 21 | 22 | -------------------------------------------------------------------------------- /tests/line_patt/checkLinePatt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "checkLinePatt.h" 4 | 5 | #define TRUE 1 6 | #define FALSE 0 7 | 8 | int isStraightLinePatt(char *patt, int pattSz) 9 | { 10 | int i, j; 11 | int offset; 12 | int pattern_offset_fits; 13 | char *new_patt; 14 | int xP, yP; 15 | int xPrior; 16 | int isStraight; 17 | int x; 18 | 19 | // format pattern 20 | offset = pattSz; 21 | 22 | for (j=1; j < pattSz; j++) // find offset 23 | { 24 | pattern_offset_fits = TRUE; 25 | 26 | for (i=j; i < pattSz; i++) 27 | if (patt[i] != patt[i-j]) 28 | { 29 | pattern_offset_fits = FALSE; 30 | break; 31 | } 32 | 33 | if (pattern_offset_fits) 34 | { 35 | offset = j; 36 | break; 37 | } 38 | } 39 | 40 | 41 | new_patt = (char *)malloc(offset); 42 | 43 | for (i=0; i < offset; i++) // make loopable 44 | new_patt[i] = patt[i]; 45 | 46 | 47 | // get the x and y coordinates 48 | xP = 0; 49 | yP = offset; 50 | for (i=0; i < offset; i++) 51 | if (new_patt[i] == 1) 52 | xP += 1; 53 | 54 | // determine if the pattern can build a straight line 55 | for (j=1; j < yP+1; j++) 56 | { 57 | xPrior = (int)((double)xP * (j-1) / yP); 58 | isStraight = TRUE; 59 | 60 | for (i=0; i < yP; i++) 61 | { 62 | x = (int)((double)xP * (j+i) / yP); 63 | 64 | if (x - xPrior != new_patt[i]) 65 | { 66 | isStraight = FALSE; 67 | break; 68 | } 69 | 70 | xPrior = x; 71 | } 72 | 73 | if (isStraight) 74 | { 75 | free(new_patt); 76 | return 1; 77 | } 78 | } 79 | 80 | free(new_patt); 81 | return 0; 82 | } 83 | 84 | int checkPattEndBit(char *patt, int pattSz, int endBit, int *bitsAllowed) 85 | { 86 | int saveBit; 87 | int ret = 0; 88 | 89 | *bitsAllowed = NONE; 90 | 91 | saveBit = patt[pattSz-1]; 92 | 93 | patt[pattSz-1] = 1; // Try 1 94 | 95 | if (isStraightLinePatt(patt, pattSz)) 96 | *bitsAllowed = 1; 97 | 98 | patt[pattSz-1] = 0; // Try 0 99 | 100 | if (isStraightLinePatt(patt, pattSz)) 101 | { 102 | if (*bitsAllowed == 1) 103 | *bitsAllowed = ANY; // either bit can make a straight line 104 | else 105 | *bitsAllowed = 0; 106 | } 107 | 108 | patt[pattSz-1] = saveBit; // return state 109 | 110 | if (endBit == *bitsAllowed) 111 | ret = BITS_MATCH; 112 | 113 | if (*bitsAllowed != NONE) 114 | ret += IS_STRAIGHT; 115 | 116 | return ret; 117 | } 118 | -------------------------------------------------------------------------------- /tests/line_patt/checkLinePatt.h: -------------------------------------------------------------------------------- 1 | #define NO_MATCH 0 2 | #define BITS_MATCH 1 3 | #define NOT_A_STRAIGHT_LINE 0 4 | #define IS_STRAIGHT 2 5 | #define NONE -1 6 | #define ANY 2 7 | 8 | int isStraightLinePatt(char *, int); 9 | int checkPattEndBit(char *, int, int, int *); 10 | -------------------------------------------------------------------------------- /tests/line_patt/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "rastToVec.h" 4 | #include "checkLinePatt.h" 5 | 6 | #define TRUE 1 7 | #define FALSE 0 8 | 9 | void drawEdgeV(int xA, int yA, int xB, int yB, int W, int H, 10 | char shade1, char shade2, char (*raster)[W]) 11 | { 12 | int x, y; 13 | 14 | for (y=yA; y < yB; y++) 15 | for (x=0; x < W; x++) 16 | if (x < (float)(xB - xA) * (y - yA) / (yB - yA) + xA) 17 | raster[y][x] = shade1; 18 | else 19 | raster[y][x] = shade2; 20 | } 21 | 22 | void getEdge(int W, int H, char (*raster)[W]) 23 | { 24 | static int t = 0, m = 0, b = 0; 25 | int xA, yA, xB, yB, xC, yC; 26 | 27 | xA = t; // top 28 | yA = H; 29 | 30 | xB = m % W; // middle 31 | yB = m / W; 32 | 33 | xC = b; // bottom 34 | yC = 0; 35 | 36 | drawEdgeV(xB, yB, xA, yA, W, yA-yB, 0, 1, raster); 37 | drawEdgeV(xC, yC, xB, yB, W, yB-yC, 0, 1, raster); 38 | 39 | t++; 40 | if (t == W) 41 | { 42 | b++; 43 | t = 0; 44 | } 45 | if (b == W) 46 | { 47 | m++; 48 | b = 0; 49 | } 50 | if (m == W*H) 51 | m = 0; 52 | } 53 | 54 | void printEdge(int W, int H, char (*raster)[W]) 55 | { 56 | int x, y; 57 | 58 | for (y=H-1; y >= 0; y--) 59 | { 60 | for (x=0; x < W; x++) 61 | printf("%i", raster[y][x]); 62 | 63 | printf("\n"); 64 | } 65 | printf("\n"); 66 | } 67 | 68 | int scanEdge(int W, int H, char (*raster)[W], int offset, int *loc) 69 | { 70 | int i; 71 | 72 | for (i=1; i < W; i++) 73 | if (raster[offset][i-1] != raster[offset][i]) 74 | { 75 | *loc = i; 76 | return 0; 77 | } 78 | 79 | return 1; 80 | } 81 | 82 | int traceEdge(int W, int H, char (*raster)[W], 83 | char *pB, char *cB, int offset, int loc, int *lineSz) 84 | { 85 | int i; 86 | char *pattBuff; 87 | char *checkBuff; 88 | struct pattern_variables *pv; 89 | int bitsAllowed; 90 | int retCheckVal; 91 | int lineInc = 0; 92 | int retVal = 0; 93 | 94 | pattBuff = pB; 95 | checkBuff = cB; 96 | pv = iniPattVars(pattBuff); 97 | 98 | for (i=offset+1; i < H; i++) 99 | { 100 | *lineSz = lineInc; 101 | lineInc++; 102 | 103 | if ((loc < W) && (raster[i][loc-1] != raster[i][loc])) 104 | { 105 | // return if "not a line" and both functions agree 106 | if (retVal) 107 | { 108 | checkBuff[lineInc-1] = 0; 109 | retCheckVal = checkPattEndBit(checkBuff, lineInc, pv->checkVal, &bitsAllowed); 110 | 111 | if ((retCheckVal&IS_STRAIGHT) == FALSE) 112 | return 0; 113 | } 114 | 115 | retVal = checkLinePatt(0, pv); 116 | 117 | checkBuff[lineInc-1] = 0; 118 | retCheckVal = checkPattEndBit(checkBuff, lineInc, pv->checkVal, &bitsAllowed); 119 | 120 | // return if both functions disagree on the possible bit values 121 | if ((retCheckVal&BITS_MATCH) == FALSE) 122 | return 1; 123 | 124 | continue; 125 | } 126 | else if ((loc+1 < W) && (raster[i][loc] != raster[i][loc+1])) 127 | { 128 | // return if "not a line" and both functions agree 129 | if (retVal) 130 | { 131 | checkBuff[lineInc-1] = 1; 132 | retCheckVal = checkPattEndBit(checkBuff, lineInc, pv->checkVal, &bitsAllowed); 133 | 134 | if ((retCheckVal&IS_STRAIGHT) == FALSE) 135 | return 0; 136 | } 137 | 138 | retVal = checkLinePatt(1, pv); 139 | 140 | checkBuff[lineInc-1] = 1; 141 | retCheckVal = checkPattEndBit(checkBuff, lineInc, pv->checkVal, &bitsAllowed); 142 | 143 | // return if both functions disagree on the possible bit values 144 | if ((retCheckVal&BITS_MATCH) == FALSE) 145 | return 1; 146 | 147 | loc++; 148 | continue; 149 | } 150 | 151 | break; 152 | } 153 | 154 | return 0; 155 | } 156 | 157 | void printPattBuffs(char *pattBuff, char *checkBuff, int size) 158 | { 159 | int i; 160 | 161 | printf("PB:"); 162 | for (i=0; i < size; i++) 163 | printf("%i", (int)pattBuff[i]); 164 | printf("\n"); 165 | 166 | printf("CB:"); 167 | for (i=0; i < size; i++) 168 | printf("%i", (int)checkBuff[i]); 169 | printf("\n\n\n"); 170 | } 171 | 172 | int main(int argc, char **argv) 173 | { 174 | #define W 20 175 | #define H 50 176 | #define OFFSET 5 177 | int i, loc; 178 | char raster[H][W]; 179 | char pB[H], cB[H]; 180 | int lineSz; 181 | 182 | for (i=0; i < W*W * W*H; i++) 183 | { 184 | getEdge(W, H, raster); 185 | printEdge(W, H, raster); // comment out to speed up computation 186 | 187 | if (scanEdge(W, H, raster, OFFSET, &loc)) 188 | continue; 189 | 190 | if (traceEdge(W, H, raster, pB, cB, OFFSET, loc, &lineSz)) 191 | { 192 | printPattBuffs(pB, cB, lineSz-1); 193 | printf("mismatch at %i\n", i); 194 | return 0; 195 | } 196 | 197 | printPattBuffs(pB, cB, lineSz-1); // comment out to speed up computation 198 | } 199 | 200 | printf("-- Test Complete -- No Errors Found --\n"); 201 | return 0; 202 | } 203 | -------------------------------------------------------------------------------- /tests/line_patt/rastToVec.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Crailin Mogged 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include "rastToVec.h" 20 | 21 | 22 | struct pattern_variables *iniPattVars(char *buff) 23 | { 24 | static struct pattern_variables pv; 25 | 26 | pv.buff = buff; 27 | pv.buffInc = 0; 28 | pv.ptrA = 0; 29 | pv.ptrB = 0; 30 | pv.cntA = 0; 31 | pv.cntB = 0; 32 | pv.incA = 0; 33 | pv.incB = 0; 34 | pv.multInc = 0; 35 | pv.multPrior = 0; 36 | pv.multH1 = 0; 37 | pv.multH2 = 0; 38 | pv.multA = 0; 39 | pv.multB = 0; 40 | pv.multOffset = 0; 41 | pv.identiSegInc = 0; 42 | pv.flipFlopSegInc = 0; 43 | pv.checkVal = 2; // 2 == any bit value; -1 == none 44 | 45 | return &pv; 46 | } 47 | 48 | 49 | void recalcBitPattVars(struct pattern_variables *pv) 50 | { 51 | int bitCntA, bitCntB; 52 | 53 | bitCntA = pv->multA * pv->cntA + pv->cntB; 54 | bitCntB = pv->multB * pv->cntA + pv->cntB; 55 | pv->ptrA = pv->buffInc+1 - (bitCntA + bitCntB); 56 | pv->ptrB = pv->buffInc+1 - bitCntB; 57 | pv->cntA = bitCntA; 58 | pv->cntB = bitCntB; 59 | } 60 | 61 | 62 | void accOffset(struct pattern_variables *pv) // account offset 63 | { 64 | if (pv->flipFlopSegInc == 0) 65 | { 66 | pv->multOffset = pv->identiSegInc + 1; 67 | pv->multInc = 0; 68 | pv->multH1 = 0; 69 | pv->multH2 = 0; 70 | pv->flipFlopSegInc = 0; 71 | } 72 | else if (pv->flipFlopSegInc == 1) 73 | { 74 | if (pv->multOffset) 75 | pv->multOffset = 0; 76 | else 77 | pv->multOffset = 1; 78 | 79 | pv->multInc = pv->identiSegInc + 1; 80 | pv->multH1 = pv->multInc; 81 | pv->multH2 = 0; 82 | pv->flipFlopSegInc = 0; 83 | } 84 | else if (pv->flipFlopSegInc == 2) 85 | { 86 | if (pv->multOffset) 87 | pv->multOffset = 1; 88 | else 89 | pv->multOffset = 0; 90 | 91 | pv->multInc = 2; 92 | pv->multH1 = 1; 93 | pv->multH2 = 2; 94 | pv->flipFlopSegInc = 1; 95 | } 96 | else if (pv->flipFlopSegInc > 2) 97 | { 98 | pv->multA = 1; 99 | pv->multB = 2; 100 | recalcBitPattVars(pv); 101 | pv->multOffset = pv->flipFlopSegInc - 1; 102 | pv->multInc = 0; 103 | pv->multH1 = 0; 104 | pv->multH2 = 0; 105 | pv->flipFlopSegInc = 0; 106 | } 107 | 108 | pv->multA = 0; 109 | pv->multB = 0; 110 | pv->identiSegInc = 0; 111 | } 112 | 113 | 114 | void recalcPattVars(struct pattern_variables *pv) 115 | { 116 | // account offsets that represent current largest possible 117 | if (pv->multInc == pv->multOffset-1) 118 | { 119 | if (pv->multH1 == 0) 120 | { 121 | pv->multH1 = pv->multOffset; 122 | pv->multOffset = 0; 123 | } 124 | else if (pv->multH2 == 0) 125 | { 126 | pv->multH1 = pv->multOffset; 127 | pv->multH2 = pv->multInc; 128 | pv->multA = pv->multOffset; 129 | pv->identiSegInc += 1; 130 | } 131 | } 132 | 133 | // account multiplier holders 134 | if (pv->multH1 == 0) 135 | pv->multH1 = pv->multInc; 136 | else if (pv->multH2 == 0) 137 | { 138 | if (pv->multInc != pv->multH1) 139 | pv->multH2 = pv->multInc; 140 | } 141 | 142 | // account both multA/B 143 | if (pv->multA == 0) 144 | { 145 | if (pv->multInc == pv->multPrior) 146 | pv->multA = pv->multInc; 147 | } 148 | else 149 | { 150 | if (pv->multInc != pv->multA) 151 | { 152 | pv->multB = pv->multInc; 153 | recalcBitPattVars(pv); 154 | } 155 | } 156 | 157 | if (pv->multB) 158 | accOffset(pv); 159 | else 160 | { 161 | // keep track of identical segments 162 | if (pv->multInc == pv->multPrior) 163 | pv->identiSegInc += 1; 164 | 165 | // keep track of the flip-flop pattern size 166 | if ((pv->multInc == pv->multH2) && (pv->multInc != pv->multPrior)) 167 | pv->flipFlopSegInc += 1; 168 | } 169 | } 170 | 171 | 172 | void growPattModel(int bitVal, struct pattern_variables *pv) 173 | { 174 | // find if the pattern matches 'A' or 'B' 175 | if (bitVal == pv->buff[pv->ptrA+pv->incA]) 176 | { 177 | if (pv->incA != -1) 178 | pv->incA += 1; 179 | } 180 | else 181 | pv->incA = -1; // does not match 182 | 183 | if (bitVal == pv->buff[pv->ptrB+pv->incB]) 184 | { 185 | if (pv->incB != -1) 186 | pv->incB += 1; 187 | } 188 | else 189 | pv->incB = -1; // does not match 190 | 191 | if (pv->incA == pv->cntA) // if the pattern matches 'A' 192 | { 193 | pv->multInc += 1; 194 | pv->incA = 0; 195 | pv->incB = 0; 196 | } 197 | 198 | if (pv->incB == pv->cntB) // if the pattern matches 'B' 199 | { 200 | recalcPattVars(pv); 201 | pv->multPrior = pv->multInc; 202 | pv->multInc = 0; 203 | pv->incA = 0; 204 | pv->incB = 0; 205 | } 206 | 207 | pv->buff[pv->buffInc] = bitVal; 208 | pv->buffInc += 1; 209 | } 210 | 211 | 212 | void resolveDuoBitVal(struct pattern_variables *pv) 213 | { 214 | if ((pv->incA != -1) && (pv->incB != -1)) 215 | { 216 | if (pv->buff[pv->ptrA+pv->incA] == pv->buff[pv->ptrB+pv->incB]) 217 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 218 | else 219 | pv->checkVal = 2; 220 | } 221 | else if (pv->incA != -1) 222 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 223 | else if (pv->incB != -1) 224 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 225 | } 226 | 227 | void duoPattCheck(struct pattern_variables *pv, int smMult, int lgMult) 228 | { 229 | if (pv->multInc == smMult) 230 | resolveDuoBitVal(pv); 231 | else if (pv->multInc == lgMult) 232 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 233 | else if (pv->multInc < smMult) 234 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 235 | } 236 | 237 | void trioPattCheck(struct pattern_variables *pv, int smMult, int medMult, int lgMult) 238 | { 239 | if (pv->multInc == smMult) 240 | resolveDuoBitVal(pv); 241 | else if (pv->multInc == medMult) 242 | resolveDuoBitVal(pv); 243 | else if (pv->multInc == lgMult) 244 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 245 | else if (pv->multInc < smMult) 246 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 247 | } 248 | 249 | void flipFlopPattCheck(struct pattern_variables *pv, int mult1, int mult2) 250 | { 251 | if (pv->multA == mult1) 252 | { 253 | if (pv->multInc == mult2) 254 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 255 | else if (pv->multInc < mult2) 256 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 257 | } 258 | else if (pv->multA == mult2) 259 | { 260 | if (pv->multInc == mult1) 261 | pv->checkVal = pv->buff[pv->ptrB+pv->incB]; 262 | else if (pv->multInc < mult1) 263 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 264 | } 265 | } 266 | 267 | int checkBit(int bitVal, struct pattern_variables *pv) 268 | { 269 | pv->checkVal = -1; 270 | 271 | if ((pv->incA == -1) && (pv->incB == -1)) 272 | return 1; 273 | 274 | if ((pv->flipFlopSegInc >= 2) && (pv->multA)) // end of flip-flop pattern 275 | { 276 | flipFlopPattCheck(pv, pv->multH1, pv->multH2); 277 | } 278 | else if (pv->multH2) // check for 2 possible patterns 279 | { 280 | if (pv->multH1 == pv->multH2-1) // multH1 < multH2 281 | duoPattCheck(pv, pv->multH1, pv->multH2); 282 | else if (pv->multH2 == pv->multH1-1) // multH2 < multH1 283 | duoPattCheck(pv, pv->multH2, pv->multH1); 284 | } 285 | else if ((pv->multH1) && (pv->multH2 == 0)) // check for 3 possible patterns 286 | { 287 | trioPattCheck(pv, pv->multH1-1, pv->multH1, pv->multH1+1); 288 | } 289 | else if (pv->multH1 == 0) // pattern size is unknown 290 | { 291 | if (pv->multInc >= pv->multOffset-1) 292 | resolveDuoBitVal(pv); 293 | else if (pv->multInc < pv->multOffset-1) 294 | pv->checkVal = pv->buff[pv->ptrA+pv->incA]; 295 | } 296 | 297 | if (bitVal == pv->checkVal) 298 | return 0; 299 | 300 | if (pv->checkVal == 2) 301 | return 0; 302 | 303 | return 1; 304 | } 305 | 306 | 307 | void iniGrowPattModel(int bitVal, struct pattern_variables *pv) 308 | { 309 | // find the bit value that occurs more often 310 | if (pv->cntA == 0) 311 | { 312 | if ((pv->buffInc) && (bitVal == pv->buff[pv->buffInc-1])) 313 | { 314 | pv->ptrA = pv->buffInc-1; 315 | pv->cntA = 1; 316 | pv->flipFlopSegInc = pv->buffInc >> 1; 317 | } 318 | } 319 | else if (pv->cntB == 0) // find the other bit 320 | { 321 | if (bitVal != pv->buff[pv->ptrA]) 322 | { 323 | pv->ptrB = pv->buffInc; 324 | pv->cntB = 1; 325 | 326 | // find offset 327 | if (pv->flipFlopSegInc == 0) // 00...1 328 | { 329 | pv->multOffset = pv->buffInc; 330 | } 331 | else if (pv->flipFlopSegInc == 1) // 0100...1 332 | { 333 | if (pv->buff[0] == pv->buff[pv->ptrA]) // 0100...1 else 100...1 334 | pv->multOffset = 1; 335 | 336 | pv->multPrior = pv->ptrB - pv->ptrA; 337 | pv->multH1 = pv->multPrior; 338 | pv->flipFlopSegInc = 0; 339 | } 340 | else if (pv->flipFlopSegInc == 2) // if flip-flop Pattern 0101001 341 | { 342 | if (pv->buff[0] == pv->buff[pv->ptrA]) // 0101001 else 101001 343 | pv->multOffset = 1; 344 | 345 | pv->multPrior = 2; 346 | pv->multH1 = 1; 347 | pv->multH2 = 2; 348 | pv->flipFlopSegInc = 1; 349 | } 350 | else if (pv->flipFlopSegInc > 2) // if flip-flop Pattern 010101...001 351 | { 352 | pv->ptrA -= 2; pv->ptrB -= 2; 353 | pv->cntA = 2; pv->cntB = 3; 354 | pv->multOffset = pv->flipFlopSegInc - 1; 355 | pv->flipFlopSegInc = 0; 356 | } 357 | } 358 | } 359 | 360 | pv->buff[pv->buffInc] = bitVal; 361 | pv->buffInc += 1; 362 | } 363 | 364 | int iniCheckBit(int bitVal, struct pattern_variables *pv) 365 | { 366 | pv->checkVal = 2; 367 | 368 | // find the possible bit values for flip-flop patterns 369 | if (pv->flipFlopSegInc >= 2) 370 | { 371 | if (pv->buff[pv->buffInc-2] == pv->buff[pv->ptrA]) 372 | pv->checkVal = !pv->buff[pv->ptrA]; 373 | 374 | if (pv->buff[pv->buffInc-3] == pv->buff[pv->ptrA]) 375 | pv->checkVal = -1; 376 | } 377 | 378 | if (bitVal == pv->checkVal) 379 | return 0; 380 | 381 | if (pv->checkVal == 2) 382 | return 0; 383 | 384 | return 1; 385 | } 386 | 387 | 388 | int checkLinePatt(int bitVal, struct pattern_variables *pv) 389 | { 390 | if (pv->cntB) 391 | { 392 | // check if the given pattern bit value is allowed 393 | if (checkBit(bitVal, pv)) 394 | return 1; 395 | 396 | // grow the pattern model that will be used to tell if the line is straight 397 | growPattModel(bitVal, pv); 398 | } 399 | else 400 | { 401 | if (iniCheckBit(bitVal, pv)) 402 | return 1; 403 | 404 | iniGrowPattModel(bitVal, pv); 405 | } 406 | 407 | return 0; 408 | } 409 | -------------------------------------------------------------------------------- /tests/line_patt/rastToVec.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Crailin Mogged 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | struct pattern_variables 18 | { 19 | char *buff; 20 | int buffInc; 21 | int ptrA; // 'A' occurs more often than 'B' 22 | int ptrB; 23 | int cntA; 24 | int cntB; 25 | int incA; 26 | int incB; 27 | int multInc; // if incA == cntA then multInc++ 28 | int multPrior; 29 | int multH1; // holds multA/B until they can be distinguished 30 | int multH2; 31 | int multA; // occurs more often than multB 32 | int multB; 33 | int multOffset; 34 | int identiSegInc; // identical segment increment 35 | int flipFlopSegInc; 36 | int checkVal; 37 | }; 38 | 39 | // iniPattVars() requires a pointer to a buffer with 40 | // the size of the window width for vertical edges 41 | // or window height for horizontal edges 42 | struct pattern_variables *iniPattVars(char *); 43 | 44 | int checkLinePatt(int, struct pattern_variables *); 45 | --------------------------------------------------------------------------------