From: nyatla Date: Sun, 14 Sep 2008 15:42:21 +0000 (+0000) Subject: [TAG]NyARToolkit-2.0.0 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=678e5469b61962cc81bc36f71bddf03582c4d587;p=nyartoolkit-and%2Fnyartoolkit-and.git [TAG]NyARToolkit-2.0.0 git-svn-id: http://svn.sourceforge.jp/svnroot/nyartoolkit/NyARToolkit@161 7cac0a50-4618-4814-88d0-24b83990f816 --- diff --git a/tags/2.0.0/Data/320x240ABGR.raw b/tags/2.0.0/Data/320x240ABGR.raw new file mode 100644 index 0000000..b5be070 Binary files /dev/null and b/tags/2.0.0/Data/320x240ABGR.raw differ diff --git a/tags/2.0.0/Data/camera_para.dat b/tags/2.0.0/Data/camera_para.dat new file mode 100644 index 0000000..eb671b1 Binary files /dev/null and b/tags/2.0.0/Data/camera_para.dat differ diff --git a/tags/2.0.0/Data/patt.hiro b/tags/2.0.0/Data/patt.hiro new file mode 100644 index 0000000..7f78e3c --- /dev/null +++ b/tags/2.0.0/Data/patt.hiro @@ -0,0 +1,196 @@ + 234 235 240 233 240 234 240 235 240 237 240 238 240 240 240 232 + 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 228 + 227 240 240 240 240 240 240 240 240 240 240 240 240 240 240 239 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 236 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 234 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 236 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 231 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 225 149 240 240 186 216 225 174 240 240 240 237 238 240 240 240 + 150 107 238 231 75 208 115 147 238 228 223 226 237 180 226 240 + 150 62 181 213 62 187 113 169 197 72 29 237 120 50 53 207 + 149 63 47 78 53 184 113 101 142 5 150 150 45 217 186 83 + 121 84 220 222 58 180 121 92 128 109 237 124 155 232 161 64 + 149 71 240 240 76 210 98 109 122 108 240 129 51 119 161 155 + 149 186 240 240 98 219 135 152 207 191 236 227 152 77 175 209 + 235 235 240 233 240 234 240 235 240 236 240 238 240 240 240 240 + 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 227 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 236 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 234 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 236 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 232 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 225 156 240 240 186 216 225 186 240 240 240 240 240 240 240 240 + 150 117 240 231 72 206 115 162 240 232 223 237 240 180 226 240 + 150 74 187 213 51 184 103 168 197 78 29 237 120 50 53 216 + 144 77 51 74 61 184 106 101 142 5 150 152 52 217 186 85 + 117 89 219 219 65 184 121 92 128 100 236 125 156 240 170 73 + 148 71 240 240 76 210 109 109 121 99 240 137 51 120 166 164 + 140 186 240 240 98 220 150 156 207 192 236 230 152 77 176 212 + 234 235 240 233 240 234 240 235 240 236 240 238 240 240 240 233 + 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 239 + 227 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 234 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 232 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 235 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 232 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 228 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 225 156 240 240 182 212 225 180 240 240 240 240 240 240 240 240 + 150 116 238 228 66 205 115 151 238 236 225 240 240 180 226 240 + 156 84 186 211 47 184 109 170 200 92 30 240 120 50 53 216 + 147 83 51 73 50 184 106 110 148 17 151 150 45 217 186 85 + 127 98 219 219 58 179 109 101 128 107 237 125 155 240 163 72 + 155 86 240 240 76 201 85 108 121 95 232 137 51 118 153 155 + 149 189 240 240 98 220 141 154 206 178 235 230 152 77 175 209 + + 232 228 239 240 240 240 240 240 240 240 240 207 83 64 155 209 + 240 240 240 240 240 240 240 240 240 240 226 53 186 161 161 175 + 240 240 240 240 240 240 240 240 240 240 180 50 217 232 119 77 + 240 240 240 240 240 240 240 240 240 238 237 120 45 155 51 152 + 238 240 240 240 240 240 240 240 240 237 226 237 150 124 129 227 + 240 240 240 240 240 240 240 240 240 240 223 29 150 237 240 236 + 237 240 240 240 240 240 240 240 240 240 228 72 5 109 108 191 + 240 240 240 240 240 240 240 240 240 240 238 197 142 128 122 207 + 235 240 240 240 240 240 240 240 240 174 147 169 101 92 109 152 + 240 240 240 240 240 240 240 240 240 225 115 113 113 121 98 135 + 234 240 240 240 240 240 240 240 240 216 208 187 184 180 210 219 + 240 240 240 240 240 240 240 240 240 186 75 62 53 58 76 98 + 233 240 240 240 240 240 240 240 240 240 231 213 78 222 240 240 + 240 240 240 240 240 240 240 240 240 240 238 181 47 220 240 240 + 235 240 240 240 240 240 240 240 240 149 107 62 63 84 71 186 + 234 229 227 240 236 234 236 231 229 225 150 150 149 121 149 149 + 240 240 240 240 240 240 240 240 240 240 240 216 85 73 164 212 + 240 240 240 240 240 240 240 240 240 240 226 53 186 170 166 176 + 240 240 240 240 240 240 240 240 240 240 180 50 217 240 120 77 + 240 240 240 240 240 240 240 240 240 240 240 120 52 156 51 152 + 238 240 240 240 240 240 240 240 240 240 237 237 152 125 137 230 + 240 240 240 240 240 240 240 240 240 240 223 29 150 236 240 236 + 236 240 240 240 240 240 240 240 240 240 232 78 5 100 99 192 + 240 240 240 240 240 240 240 240 240 240 240 197 142 128 121 207 + 235 240 240 240 240 240 240 240 240 186 162 168 101 92 109 156 + 240 240 240 240 240 240 240 240 240 225 115 103 106 121 109 150 + 234 240 240 240 240 240 240 240 240 216 206 184 184 184 210 220 + 240 240 240 240 240 240 240 240 240 186 72 51 61 65 76 98 + 233 240 240 240 240 240 240 240 240 240 231 213 74 219 240 240 + 240 240 240 240 240 240 240 240 240 240 240 187 51 219 240 240 + 235 240 240 240 240 240 240 240 240 156 117 74 77 89 71 186 + 235 229 227 240 236 234 236 232 229 225 150 150 144 117 148 140 + 233 239 240 240 240 240 240 240 240 240 240 216 85 72 155 209 + 240 240 240 240 240 240 240 240 240 240 226 53 186 163 153 175 + 240 240 240 240 240 240 240 240 240 240 180 50 217 240 118 77 + 240 240 240 240 240 240 240 240 240 240 240 120 45 155 51 152 + 238 240 240 240 240 240 240 240 240 240 240 240 150 125 137 230 + 240 240 240 240 240 240 240 240 240 240 225 30 151 237 232 235 + 236 240 240 240 240 240 240 240 240 240 236 92 17 107 95 178 + 240 240 240 240 240 240 240 240 240 240 238 200 148 128 121 206 + 235 240 240 240 240 240 240 240 240 180 151 170 110 101 108 154 + 240 240 240 240 240 240 240 240 240 225 115 109 106 109 85 141 + 234 240 240 240 240 240 240 240 240 212 205 184 184 179 201 220 + 240 240 240 240 240 240 240 240 240 182 66 47 50 58 76 98 + 233 240 240 240 240 240 240 240 240 240 228 211 73 219 240 240 + 240 240 240 240 240 240 240 240 240 240 238 186 51 219 240 240 + 235 240 240 240 240 240 240 240 240 156 116 84 83 98 86 189 + 234 229 227 240 234 232 235 232 228 225 150 156 147 127 155 149 + + 209 175 77 152 227 236 191 207 152 135 219 98 240 240 186 149 + 155 161 119 51 129 240 108 122 109 98 210 76 240 240 71 149 + 64 161 232 155 124 237 109 128 92 121 180 58 222 220 84 121 + 83 186 217 45 150 150 5 142 101 113 184 53 78 47 63 149 + 207 53 50 120 237 29 72 197 169 113 187 62 213 181 62 150 + 240 226 180 237 226 223 228 238 147 115 208 75 231 238 107 150 + 240 240 240 238 237 240 240 240 174 225 216 186 240 240 149 225 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 231 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 236 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 234 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 236 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 239 240 240 240 240 240 240 240 240 240 240 240 240 240 240 227 + 228 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 + 232 240 240 240 238 240 237 240 235 240 234 240 233 240 235 234 + 212 176 77 152 230 236 192 207 156 150 220 98 240 240 186 140 + 164 166 120 51 137 240 99 121 109 109 210 76 240 240 71 148 + 73 170 240 156 125 236 100 128 92 121 184 65 219 219 89 117 + 85 186 217 52 152 150 5 142 101 106 184 61 74 51 77 144 + 216 53 50 120 237 29 78 197 168 103 184 51 213 187 74 150 + 240 226 180 240 237 223 232 240 162 115 206 72 231 240 117 150 + 240 240 240 240 240 240 240 240 186 225 216 186 240 240 156 225 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 232 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 236 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 234 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 236 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 227 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 + 240 240 240 240 238 240 236 240 235 240 234 240 233 240 235 235 + 209 175 77 152 230 235 178 206 154 141 220 98 240 240 189 149 + 155 153 118 51 137 232 95 121 108 85 201 76 240 240 86 155 + 72 163 240 155 125 237 107 128 101 109 179 58 219 219 98 127 + 85 186 217 45 150 151 17 148 110 106 184 50 73 51 83 147 + 216 53 50 120 240 30 92 200 170 109 184 47 211 186 84 156 + 240 226 180 240 240 225 236 238 151 115 205 66 228 238 116 150 + 240 240 240 240 240 240 240 240 180 225 212 182 240 240 156 225 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 228 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 232 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 235 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 232 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 234 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 227 + 239 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 + 233 240 240 240 238 240 236 240 235 240 234 240 233 240 235 234 + + 149 149 121 149 150 150 225 229 231 236 234 236 240 227 229 234 + 186 71 84 63 62 107 149 240 240 240 240 240 240 240 240 235 + 240 240 220 47 181 238 240 240 240 240 240 240 240 240 240 240 + 240 240 222 78 213 231 240 240 240 240 240 240 240 240 240 233 + 98 76 58 53 62 75 186 240 240 240 240 240 240 240 240 240 + 219 210 180 184 187 208 216 240 240 240 240 240 240 240 240 234 + 135 98 121 113 113 115 225 240 240 240 240 240 240 240 240 240 + 152 109 92 101 169 147 174 240 240 240 240 240 240 240 240 235 + 207 122 128 142 197 238 240 240 240 240 240 240 240 240 240 240 + 191 108 109 5 72 228 240 240 240 240 240 240 240 240 240 237 + 236 240 237 150 29 223 240 240 240 240 240 240 240 240 240 240 + 227 129 124 150 237 226 237 240 240 240 240 240 240 240 240 238 + 152 51 155 45 120 237 238 240 240 240 240 240 240 240 240 240 + 77 119 232 217 50 180 240 240 240 240 240 240 240 240 240 240 + 175 161 161 186 53 226 240 240 240 240 240 240 240 240 240 240 + 209 155 64 83 207 240 240 240 240 240 240 240 240 239 228 232 + 140 148 117 144 150 150 225 229 232 236 234 236 240 227 229 235 + 186 71 89 77 74 117 156 240 240 240 240 240 240 240 240 235 + 240 240 219 51 187 240 240 240 240 240 240 240 240 240 240 240 + 240 240 219 74 213 231 240 240 240 240 240 240 240 240 240 233 + 98 76 65 61 51 72 186 240 240 240 240 240 240 240 240 240 + 220 210 184 184 184 206 216 240 240 240 240 240 240 240 240 234 + 150 109 121 106 103 115 225 240 240 240 240 240 240 240 240 240 + 156 109 92 101 168 162 186 240 240 240 240 240 240 240 240 235 + 207 121 128 142 197 240 240 240 240 240 240 240 240 240 240 240 + 192 99 100 5 78 232 240 240 240 240 240 240 240 240 240 236 + 236 240 236 150 29 223 240 240 240 240 240 240 240 240 240 240 + 230 137 125 152 237 237 240 240 240 240 240 240 240 240 240 238 + 152 51 156 52 120 240 240 240 240 240 240 240 240 240 240 240 + 77 120 240 217 50 180 240 240 240 240 240 240 240 240 240 240 + 176 166 170 186 53 226 240 240 240 240 240 240 240 240 240 240 + 212 164 73 85 216 240 240 240 240 240 240 240 240 240 240 240 + 149 155 127 147 156 150 225 228 232 235 232 234 240 227 229 234 + 189 86 98 83 84 116 156 240 240 240 240 240 240 240 240 235 + 240 240 219 51 186 238 240 240 240 240 240 240 240 240 240 240 + 240 240 219 73 211 228 240 240 240 240 240 240 240 240 240 233 + 98 76 58 50 47 66 182 240 240 240 240 240 240 240 240 240 + 220 201 179 184 184 205 212 240 240 240 240 240 240 240 240 234 + 141 85 109 106 109 115 225 240 240 240 240 240 240 240 240 240 + 154 108 101 110 170 151 180 240 240 240 240 240 240 240 240 235 + 206 121 128 148 200 238 240 240 240 240 240 240 240 240 240 240 + 178 95 107 17 92 236 240 240 240 240 240 240 240 240 240 236 + 235 232 237 151 30 225 240 240 240 240 240 240 240 240 240 240 + 230 137 125 150 240 240 240 240 240 240 240 240 240 240 240 238 + 152 51 155 45 120 240 240 240 240 240 240 240 240 240 240 240 + 77 118 240 217 50 180 240 240 240 240 240 240 240 240 240 240 + 175 153 163 186 53 226 240 240 240 240 240 240 240 240 240 240 + 209 155 72 85 216 240 240 240 240 240 240 240 240 240 239 233 + diff --git a/tags/2.0.0/Data/patt.kanji b/tags/2.0.0/Data/patt.kanji new file mode 100644 index 0000000..87d32bb --- /dev/null +++ b/tags/2.0.0/Data/patt.kanji @@ -0,0 +1,196 @@ + 214 225 240 225 214 240 216 204 214 227 181 192 198 192 181 192 + 240 240 240 240 240 240 225 232 240 240 240 240 240 240 240 236 + 240 240 240 240 240 240 75 128 220 240 240 240 240 240 240 240 + 240 240 240 240 240 240 106 53 240 240 240 240 240 240 240 237 + 240 240 240 240 240 238 118 31 240 240 240 240 240 240 240 234 + 240 240 240 240 240 240 74 49 207 240 240 240 240 240 240 240 + 240 240 240 240 240 240 53 54 177 240 240 240 240 240 240 240 + 240 240 240 240 240 240 64 31 130 240 240 240 240 240 240 219 + 240 240 240 240 240 180 37 57 78 228 240 240 240 240 240 240 + 240 240 240 240 240 118 62 157 36 185 240 240 240 240 240 231 + 240 240 240 240 240 82 65 225 67 80 230 240 240 240 240 217 + 240 240 240 225 53 76 225 240 156 62 158 240 240 240 240 226 + 240 240 199 61 9 111 235 240 240 104 58 174 228 240 240 240 + 240 142 64 26 92 227 240 240 240 229 93 64 170 226 238 216 + 90 26 12 156 240 240 240 240 240 240 204 95 30 117 192 200 + 156 16 195 233 235 240 236 240 238 239 240 186 93 53 120 237 + 214 226 240 225 212 240 216 204 212 226 181 192 198 192 185 194 + 240 240 240 240 240 240 227 232 240 240 240 240 240 240 240 238 + 240 240 240 240 240 240 95 138 225 240 240 240 240 240 240 240 + 240 240 240 240 240 240 108 59 240 240 240 240 240 240 240 237 + 240 240 240 240 240 238 118 31 240 240 240 240 240 240 240 234 + 240 240 240 240 240 240 83 47 207 240 240 240 240 240 240 240 + 240 240 240 240 240 240 56 49 177 240 240 240 240 240 240 240 + 240 240 240 240 240 240 73 41 130 240 240 240 240 240 240 222 + 240 240 240 240 240 185 46 49 86 230 240 240 240 240 240 240 + 240 240 240 240 240 118 58 165 45 192 240 240 240 240 240 234 + 240 240 240 240 240 91 63 222 74 82 240 240 240 240 240 222 + 240 240 240 226 66 86 225 240 158 63 162 240 240 240 240 228 + 240 240 202 76 11 103 235 240 234 91 49 174 228 240 240 240 + 240 142 68 16 91 226 240 240 240 228 96 74 178 233 239 222 + 90 26 4 150 240 240 240 240 240 240 213 109 46 133 204 213 + 156 14 195 234 236 240 237 240 239 240 240 192 106 57 125 238 + 214 226 240 225 212 240 216 204 214 227 181 192 198 192 184 192 + 240 240 240 240 240 240 226 232 240 240 240 240 240 240 240 236 + 240 240 240 240 240 240 85 134 220 240 240 240 240 240 240 240 + 240 240 240 240 240 240 107 58 240 240 240 240 240 240 240 237 + 240 240 240 240 240 238 118 32 240 240 240 240 240 240 240 234 + 240 240 240 240 240 240 87 60 210 240 240 240 240 240 240 240 + 240 240 240 240 240 240 58 58 178 240 240 240 240 240 240 240 + 240 240 240 240 240 240 73 31 130 240 240 240 240 240 240 219 + 240 240 240 240 240 185 46 59 86 228 240 240 240 240 240 240 + 240 240 240 240 240 118 62 168 41 186 240 240 240 240 240 231 + 240 240 240 240 240 90 65 225 60 92 235 240 240 240 240 219 + 240 240 240 225 53 82 225 240 146 63 163 240 240 240 240 228 + 240 240 198 61 5 103 235 240 234 102 58 175 232 240 240 240 + 240 134 54 13 91 226 240 240 240 229 96 68 188 238 239 222 + 90 15 3 150 240 240 240 240 240 240 213 105 48 134 204 213 + 156 14 195 233 236 240 237 240 239 239 240 192 106 57 125 238 + + 192 236 240 237 234 240 240 219 240 231 217 226 240 216 200 237 + 181 240 240 240 240 240 240 240 240 240 240 240 240 238 192 120 + 192 240 240 240 240 240 240 240 240 240 240 240 240 226 117 53 + 198 240 240 240 240 240 240 240 240 240 240 240 228 170 30 93 + 192 240 240 240 240 240 240 240 240 240 240 240 174 64 95 186 + 181 240 240 240 240 240 240 240 240 240 230 158 58 93 204 240 + 227 240 240 240 240 240 240 240 228 185 80 62 104 229 240 239 + 214 240 220 240 240 207 177 130 78 36 67 156 240 240 240 238 + 204 232 128 53 31 49 54 31 57 157 225 240 240 240 240 240 + 216 225 75 106 118 74 53 64 37 62 65 225 235 240 240 236 + 240 240 240 240 238 240 240 240 180 118 82 76 111 227 240 240 + 214 240 240 240 240 240 240 240 240 240 240 53 9 92 240 235 + 225 240 240 240 240 240 240 240 240 240 240 225 61 26 156 233 + 240 240 240 240 240 240 240 240 240 240 240 240 199 64 12 195 + 225 240 240 240 240 240 240 240 240 240 240 240 240 142 26 16 + 214 240 240 240 240 240 240 240 240 240 240 240 240 240 90 156 + 194 238 240 237 234 240 240 222 240 234 222 228 240 222 213 238 + 185 240 240 240 240 240 240 240 240 240 240 240 240 239 204 125 + 192 240 240 240 240 240 240 240 240 240 240 240 240 233 133 57 + 198 240 240 240 240 240 240 240 240 240 240 240 228 178 46 106 + 192 240 240 240 240 240 240 240 240 240 240 240 174 74 109 192 + 181 240 240 240 240 240 240 240 240 240 240 162 49 96 213 240 + 226 240 240 240 240 240 240 240 230 192 82 63 91 228 240 240 + 212 240 225 240 240 207 177 130 86 45 74 158 234 240 240 239 + 204 232 138 59 31 47 49 41 49 165 222 240 240 240 240 240 + 216 227 95 108 118 83 56 73 46 58 63 225 235 240 240 237 + 240 240 240 240 238 240 240 240 185 118 91 86 103 226 240 240 + 212 240 240 240 240 240 240 240 240 240 240 66 11 91 240 236 + 225 240 240 240 240 240 240 240 240 240 240 226 76 16 150 234 + 240 240 240 240 240 240 240 240 240 240 240 240 202 68 4 195 + 226 240 240 240 240 240 240 240 240 240 240 240 240 142 26 14 + 214 240 240 240 240 240 240 240 240 240 240 240 240 240 90 156 + 192 236 240 237 234 240 240 219 240 231 219 228 240 222 213 238 + 184 240 240 240 240 240 240 240 240 240 240 240 240 239 204 125 + 192 240 240 240 240 240 240 240 240 240 240 240 240 238 134 57 + 198 240 240 240 240 240 240 240 240 240 240 240 232 188 48 106 + 192 240 240 240 240 240 240 240 240 240 240 240 175 68 105 192 + 181 240 240 240 240 240 240 240 240 240 235 163 58 96 213 240 + 227 240 240 240 240 240 240 240 228 186 92 63 102 229 240 239 + 214 240 220 240 240 210 178 130 86 41 60 146 234 240 240 239 + 204 232 134 58 32 60 58 31 59 168 225 240 240 240 240 240 + 216 226 85 107 118 87 58 73 46 62 65 225 235 240 240 237 + 240 240 240 240 238 240 240 240 185 118 90 82 103 226 240 240 + 212 240 240 240 240 240 240 240 240 240 240 53 5 91 240 236 + 225 240 240 240 240 240 240 240 240 240 240 225 61 13 150 233 + 240 240 240 240 240 240 240 240 240 240 240 240 198 54 3 195 + 226 240 240 240 240 240 240 240 240 240 240 240 240 134 15 14 + 214 240 240 240 240 240 240 240 240 240 240 240 240 240 90 156 + + 237 120 53 93 186 240 239 238 240 236 240 235 233 195 16 156 + 200 192 117 30 95 204 240 240 240 240 240 240 156 12 26 90 + 216 238 226 170 64 93 229 240 240 240 227 92 26 64 142 240 + 240 240 240 228 174 58 104 240 240 235 111 9 61 199 240 240 + 226 240 240 240 240 158 62 156 240 225 76 53 225 240 240 240 + 217 240 240 240 240 230 80 67 225 65 82 240 240 240 240 240 + 231 240 240 240 240 240 185 36 157 62 118 240 240 240 240 240 + 240 240 240 240 240 240 228 78 57 37 180 240 240 240 240 240 + 219 240 240 240 240 240 240 130 31 64 240 240 240 240 240 240 + 240 240 240 240 240 240 240 177 54 53 240 240 240 240 240 240 + 240 240 240 240 240 240 240 207 49 74 240 240 240 240 240 240 + 234 240 240 240 240 240 240 240 31 118 238 240 240 240 240 240 + 237 240 240 240 240 240 240 240 53 106 240 240 240 240 240 240 + 240 240 240 240 240 240 240 220 128 75 240 240 240 240 240 240 + 236 240 240 240 240 240 240 240 232 225 240 240 240 240 240 240 + 192 181 192 198 192 181 227 214 204 216 240 214 225 240 225 214 + 238 125 57 106 192 240 240 239 240 237 240 236 234 195 14 156 + 213 204 133 46 109 213 240 240 240 240 240 240 150 4 26 90 + 222 239 233 178 74 96 228 240 240 240 226 91 16 68 142 240 + 240 240 240 228 174 49 91 234 240 235 103 11 76 202 240 240 + 228 240 240 240 240 162 63 158 240 225 86 66 226 240 240 240 + 222 240 240 240 240 240 82 74 222 63 91 240 240 240 240 240 + 234 240 240 240 240 240 192 45 165 58 118 240 240 240 240 240 + 240 240 240 240 240 240 230 86 49 46 185 240 240 240 240 240 + 222 240 240 240 240 240 240 130 41 73 240 240 240 240 240 240 + 240 240 240 240 240 240 240 177 49 56 240 240 240 240 240 240 + 240 240 240 240 240 240 240 207 47 83 240 240 240 240 240 240 + 234 240 240 240 240 240 240 240 31 118 238 240 240 240 240 240 + 237 240 240 240 240 240 240 240 59 108 240 240 240 240 240 240 + 240 240 240 240 240 240 240 225 138 95 240 240 240 240 240 240 + 238 240 240 240 240 240 240 240 232 227 240 240 240 240 240 240 + 194 185 192 198 192 181 226 212 204 216 240 212 225 240 226 214 + 238 125 57 106 192 240 239 239 240 237 240 236 233 195 14 156 + 213 204 134 48 105 213 240 240 240 240 240 240 150 3 15 90 + 222 239 238 188 68 96 229 240 240 240 226 91 13 54 134 240 + 240 240 240 232 175 58 102 234 240 235 103 5 61 198 240 240 + 228 240 240 240 240 163 63 146 240 225 82 53 225 240 240 240 + 219 240 240 240 240 235 92 60 225 65 90 240 240 240 240 240 + 231 240 240 240 240 240 186 41 168 62 118 240 240 240 240 240 + 240 240 240 240 240 240 228 86 59 46 185 240 240 240 240 240 + 219 240 240 240 240 240 240 130 31 73 240 240 240 240 240 240 + 240 240 240 240 240 240 240 178 58 58 240 240 240 240 240 240 + 240 240 240 240 240 240 240 210 60 87 240 240 240 240 240 240 + 234 240 240 240 240 240 240 240 32 118 238 240 240 240 240 240 + 237 240 240 240 240 240 240 240 58 107 240 240 240 240 240 240 + 240 240 240 240 240 240 240 220 134 85 240 240 240 240 240 240 + 236 240 240 240 240 240 240 240 232 226 240 240 240 240 240 240 + 192 184 192 198 192 181 227 214 204 216 240 212 225 240 226 214 + + 156 90 240 240 240 240 240 240 240 240 240 240 240 240 240 214 + 16 26 142 240 240 240 240 240 240 240 240 240 240 240 240 225 + 195 12 64 199 240 240 240 240 240 240 240 240 240 240 240 240 + 233 156 26 61 225 240 240 240 240 240 240 240 240 240 240 225 + 235 240 92 9 53 240 240 240 240 240 240 240 240 240 240 214 + 240 240 227 111 76 82 118 180 240 240 240 238 240 240 240 240 + 236 240 240 235 225 65 62 37 64 53 74 118 106 75 225 216 + 240 240 240 240 240 225 157 57 31 54 49 31 53 128 232 204 + 238 240 240 240 156 67 36 78 130 177 207 240 240 220 240 214 + 239 240 229 104 62 80 185 228 240 240 240 240 240 240 240 227 + 240 204 93 58 158 230 240 240 240 240 240 240 240 240 240 181 + 186 95 64 174 240 240 240 240 240 240 240 240 240 240 240 192 + 93 30 170 228 240 240 240 240 240 240 240 240 240 240 240 198 + 53 117 226 240 240 240 240 240 240 240 240 240 240 240 240 192 + 120 192 238 240 240 240 240 240 240 240 240 240 240 240 240 181 + 237 200 216 240 226 217 231 240 219 240 240 234 237 240 236 192 + 156 90 240 240 240 240 240 240 240 240 240 240 240 240 240 214 + 14 26 142 240 240 240 240 240 240 240 240 240 240 240 240 226 + 195 4 68 202 240 240 240 240 240 240 240 240 240 240 240 240 + 234 150 16 76 226 240 240 240 240 240 240 240 240 240 240 225 + 236 240 91 11 66 240 240 240 240 240 240 240 240 240 240 212 + 240 240 226 103 86 91 118 185 240 240 240 238 240 240 240 240 + 237 240 240 235 225 63 58 46 73 56 83 118 108 95 227 216 + 240 240 240 240 240 222 165 49 41 49 47 31 59 138 232 204 + 239 240 240 234 158 74 45 86 130 177 207 240 240 225 240 212 + 240 240 228 91 63 82 192 230 240 240 240 240 240 240 240 226 + 240 213 96 49 162 240 240 240 240 240 240 240 240 240 240 181 + 192 109 74 174 240 240 240 240 240 240 240 240 240 240 240 192 + 106 46 178 228 240 240 240 240 240 240 240 240 240 240 240 198 + 57 133 233 240 240 240 240 240 240 240 240 240 240 240 240 192 + 125 204 239 240 240 240 240 240 240 240 240 240 240 240 240 185 + 238 213 222 240 228 222 234 240 222 240 240 234 237 240 238 194 + 156 90 240 240 240 240 240 240 240 240 240 240 240 240 240 214 + 14 15 134 240 240 240 240 240 240 240 240 240 240 240 240 226 + 195 3 54 198 240 240 240 240 240 240 240 240 240 240 240 240 + 233 150 13 61 225 240 240 240 240 240 240 240 240 240 240 225 + 236 240 91 5 53 240 240 240 240 240 240 240 240 240 240 212 + 240 240 226 103 82 90 118 185 240 240 240 238 240 240 240 240 + 237 240 240 235 225 65 62 46 73 58 87 118 107 85 226 216 + 240 240 240 240 240 225 168 59 31 58 60 32 58 134 232 204 + 239 240 240 234 146 60 41 86 130 178 210 240 240 220 240 214 + 239 240 229 102 63 92 186 228 240 240 240 240 240 240 240 227 + 240 213 96 58 163 235 240 240 240 240 240 240 240 240 240 181 + 192 105 68 175 240 240 240 240 240 240 240 240 240 240 240 192 + 106 48 188 232 240 240 240 240 240 240 240 240 240 240 240 198 + 57 134 238 240 240 240 240 240 240 240 240 240 240 240 240 192 + 125 204 239 240 240 240 240 240 240 240 240 240 240 240 240 184 + 238 213 222 240 228 219 231 240 219 240 240 234 237 240 236 192 + diff --git a/tags/2.0.0/Data/pattHiro.pdf b/tags/2.0.0/Data/pattHiro.pdf new file mode 100644 index 0000000..dcaf4f5 Binary files /dev/null and b/tags/2.0.0/Data/pattHiro.pdf differ diff --git a/tags/2.0.0/Data/pattKanji.pdf b/tags/2.0.0/Data/pattKanji.pdf new file mode 100644 index 0000000..550c721 Binary files /dev/null and b/tags/2.0.0/Data/pattKanji.pdf differ diff --git a/tags/2.0.0/LICENCE.txt b/tags/2.0.0/LICENCE.txt new file mode 100644 index 0000000..a3330c1 --- /dev/null +++ b/tags/2.0.0/LICENCE.txt @@ -0,0 +1,35 @@ +ARToolkit Java class library NyARToolkit. +Copyright (C)2008 R.Iizuka + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + + +Java版 ARToolkit クラスライブラリ NyARToolkit +Copyright (C)2008 R.Iizuka + +このプログラムはフリーソフトウェアです。あなたはこれを、フリーソフトウェ +ア財団によって発行された GNU 一般公衆利用許諾契約書(バージョン2か、希 +望によってはそれ以降のバージョンのうちどれか)の定める条件の下で再頒布 +または改変することができます。 + +このプログラムは有用であることを願って頒布されますが、*全くの無保証* +です。商業可能性の保証や特定の目的への適合性は、言外に示されたものも含 +め全く存在しません。詳しくはGNU 一般公衆利用許諾契約書をご覧ください。 + +あなたはこのプログラムと共に、GNU 一般公衆利用許諾契約書の複製物を一部 +受け取ったはずです。もし受け取っていなければ、フリーソフトウェア財団ま +で請求してください(宛先は the Free Software Foundation, Inc., 59 +Temple Place, Suite 330, Boston, MA 02111-1307 USA)。 \ No newline at end of file diff --git a/tags/2.0.0/bin/readme.txt b/tags/2.0.0/bin/readme.txt new file mode 100644 index 0000000..089f20d --- /dev/null +++ b/tags/2.0.0/bin/readme.txt @@ -0,0 +1,3 @@ +準備中です。ゴメンネ + +Not ready. \ No newline at end of file diff --git a/tags/2.0.0/readme.ja.txt b/tags/2.0.0/readme.ja.txt new file mode 100644 index 0000000..8c369da --- /dev/null +++ b/tags/2.0.0/readme.ja.txt @@ -0,0 +1,188 @@ +ARToolKit Java class library NyARToolkit. +Copyright (C)2008 R.Iizuka + +version 2.0.0 + +http://nyatla.jp/nyartoolkit/ +airmail(at)ebony.plala.or.jp +-------------------------------------------------- + + + + +・NyARToolkit/2.0 + +NyARToolkitは、Pure Javaで実装したARToolKitクラスライブラリです。 + +ARToolKit 2.72.1をベースに構築されています。 + +NyARToolkit/1.xと比較し、構造的な最適化がされ、可読性が向上しています。 + + + +J2SEでのみ動作を確認しました。 +J2MEやMIDP2.0にはそのうち対応します。 + + +ARToolkitは加藤博一先生とHuman Interface Technology Labにより +開発されたAugmented Reality (AR) ライブラリです。 +詳しくはこちらをご覧下さい。 +http://www.hitl.washington.edu/artoolkit/ + + +・NyARToolkitの特徴 + + -ARToolKitと同等な処理シーケンスを、クラスベースで再構築してあります。 + + -構造的な最適化がされており、ARToolKitと比較して可読性に優れています。 + + -座標変換の演算性能が1.5倍ほど高速です。(JIT有効時)複数マーカー取り扱い時は、 +  オリジナルよりも良い成績が得られます。 + + -取り扱える画像サイズに制限がありません。 + + -取り扱えるマーカー個数の最大値が可変です。 + + + + + + +・構成 + ++-----------------------------------------------+ +| Application | ++-------+---------+--------+--------+-----------+ +|NyARJMF|CaptureQT| NyARJoglNyARJ3d | | ++-------+---------+--------+--------+ | +| JMF |QuickTime| JOGL | Java3D |NyARToolkit| ++-------+---------+--------+--------+ | +| Camera | 3D | | +------------------------------------+-----------+ + + +映像キャプチャにはJMF、又はQuickTimeを使用することが出来ます。 + +3D描画にはJOGL又はJava3Dを使用することが出来ます。 + +NyARJMF/CaptureQT/NyARJog/NyARJ3dは、下位のキャプチャモジュール +や3Dライブラリを使いやすくするためのラッパークラス群です。 + +これらとNyARToolkitは疎結合になっており、インタフェイスをあわせる +ことで、自作モジュール等との差し替えが出来ます。 + + + + +・サンプルなど + +1.動作させる前に、JMFとJOGLかJava3Dをインストールしてください。 +  QuickTimeを使う場合には、QuickTime for Javaも必要です。 + + +動作確認したバージョンと入手先はこちらです。 + +JMF JavaTM Media Framework 2.1.1e +http://java.sun.com/products/java-media/jmf/index.jsp + +jogl-1.1.1-pre-20080328-xxxx-i586.zip +https://jogl.dev.java.net/ + +java3d-1_5_1-xxxx-i586.exe +https://java3d.dev.java.net/binary-builds.html + +QuickTime 7.5 +http://www.apple.com/quicktime/qtjava/ + + + +2.eclipseで空のワークスペースを作成し、sample,src,src.utils +  ディレクトリをインポートしてください。 + +srcにはNyARToolkit本体、src.utilsにはキャプチャや3Dライブラリとの +インタフェイスクラス群、sampleにはこれらを組み合わせた動作する +サンプルプログラムがあります。 + + + + +・プロジェクトの説明 + +ライブラリ + +NyARToolkit + NyARToolkitライブラリの本体です。依存する外部モジュールはありません。 + +NyARToolkit.utils.jmf + JMFからの画像をNyARToolkitに取り込むクラス群があります。 + 外部ライブラリは、JMFに依存します。 + +NyARToolkit.utils.qt + QuickTimeからの画像をNyARToolkitに取り込むクラス群があります。 + 外部ライブラリは、JMF、QuickTime for Javaに依存します。 + +NyARToolkit.utils.jogl + OpenGLとNyARToolkitのインタフェイスクラス群があります。 + 外部ライブラリは、JMF,JOGLに依存します。 + +NyARToolkit.utils.java3d + Java3DとNyARToolkitのインタフェイスクラス群があります。 + 外部ライブラリは、JMF,Java3Dに依存します。 + + +サンプル + +NyARToolkit.sample.jogl + JOGL/JMFを使ったサンプルプログラムがあります。 + + -jp.nyatla.nyartoolkit.jogl.sample.JavaSimpleLite + 単一のマーカーを認識するARToolkitのsimpleLite相当のサンプルです。 + -jp.nyatla.nyartoolkit.jogl.sample.JavaSimpleLite2 + 複数のマーカーを認識するサンプルです。~100個程度のマーカーを同時に + 認識します。 + + +NyARToolkit.sample.java3d + JOGL/JMFを使ったサンプルプログラムです。 +  + -jp.nyatla.nyartoolkit.java3d.sample + simpleLiteをJava3Dで動かすサンプルがあります。 + +NyARToolkit.sample.jmf + JMFを使ったサンプルプログラムです。 + + -jp.nyatla..nyartoolkit.jmf.sample + JMFでキャプチャした画像をNyARToolkitで処理するサンプルプログラムです。 +  + +NyARToolkit.sample.qt + Quicktime for Javaを使ったサンプルプログラムです。 + + -jp.nyatla.nyartoolkit.qt.sample + QuickTimeでキャプチャした画像をNyARToolkitで処理するサンプルプログラムです。 +  + + + + +・足りない機能等 + +カメラキャリブレーション、マーカーのセーブ機能等が相変わらずありません。 +今後実装していきます。 + + + + +・ライセンス +GPLv2以降です。詳しくはLICENCE.txtをみてください。 +ソースコード毎のライセンスは、各ソースファイルを参照してください。 + + + + +・謝辞 + +arc@dmzさん +http://digitalmuseum.jp/ + +QuickTimeキャプチャモジュールを提供をして頂きました。有難うございます。 \ No newline at end of file diff --git a/tags/2.0.0/sample/java3d/.classpath b/tags/2.0.0/sample/java3d/.classpath new file mode 100644 index 0000000..c862de1 --- /dev/null +++ b/tags/2.0.0/sample/java3d/.classpath @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/tags/2.0.0/sample/java3d/.project b/tags/2.0.0/sample/java3d/.project new file mode 100644 index 0000000..35755d8 --- /dev/null +++ b/tags/2.0.0/sample/java3d/.project @@ -0,0 +1,17 @@ + + + NyARToolkit.sample.java3d + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/sample/java3d/jp/nyatla/nyartoolkit/java3d/sample/NyARJava3D.java b/tags/2.0.0/sample/java3d/jp/nyatla/nyartoolkit/java3d/sample/NyARJava3D.java new file mode 100644 index 0000000..a1786c2 --- /dev/null +++ b/tags/2.0.0/sample/java3d/jp/nyatla/nyartoolkit/java3d/sample/NyARJava3D.java @@ -0,0 +1,182 @@ +/* + * PROJECT: NyARToolkit Java3d sample program. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.java3d.sample; + +import java.awt.BorderLayout; +import javax.media.j3d.*; + +import com.sun.j3d.utils.universe.*; +import java.awt.*; +import javax.swing.JFrame; +import javax.vecmath.*; + +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.java3d.utils.*; + +import com.sun.j3d.utils.geometry.ColorCube; + +/** + * Java3Dサンプルプログラム + * 単一マーカー追跡用のBehaviorを使って、背景と1個のマーカーに連動した + * TransformGroupを動かします。 + * + */ +public class NyARJava3D extends JFrame implements NyARSingleMarkerBehaviorListener +{ + private final String CARCODE_FILE = "../../Data/patt.hiro"; + + private final String PARAM_FILE = "../../Data/camera_para.dat"; + + //NyARToolkit関係 + private NyARSingleMarkerBehaviorHolder nya_behavior; + + private J3dNyARParam ar_param; + + //universe関係 + private Canvas3D canvas; + + private Locale locale; + + private VirtualUniverse universe; + + public static void main(String[] args) + { + try { + NyARJava3D frame = new NyARJava3D(); + + frame.setVisible(true); + Insets ins = frame.getInsets(); + frame.setSize(320 + ins.left + ins.right, 240 + ins.top + ins.bottom); + frame.startCapture(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void onUpdate(boolean i_is_marker_exist, Transform3D i_transform3d) + { + /* + * TODO:Please write your behavior operation code here. + * マーカーの姿勢を元に他の3Dオブジェクトを操作するときは、ここに処理を書きます。*/ + + } + + public void startCapture() throws Exception + { + nya_behavior.start(); + } + + public NyARJava3D() throws Exception + { + super("Java3D Example NyARToolkit"); + + //NyARToolkitの準備 + NyARCode ar_code = new NyARCode(16, 16); + ar_code.loadARPattFromFile(CARCODE_FILE); + ar_param = new J3dNyARParam(); + ar_param.loadARParamFromFile(PARAM_FILE); + ar_param.changeScreenSize(320, 240); + + //localeの作成とlocateとviewの設定 + universe = new VirtualUniverse(); + locale = new Locale(universe); + canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); + View view = new View(); + ViewPlatform viewPlatform = new ViewPlatform(); + view.attachViewPlatform(viewPlatform); + view.addCanvas3D(canvas); + view.setPhysicalBody(new PhysicalBody()); + view.setPhysicalEnvironment(new PhysicalEnvironment()); + + //視界の設定(カメラ設定から取得) + Transform3D camera_3d = ar_param.getCameraTransform(); + view.setCompatibilityModeEnable(true); + view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION); + view.setLeftProjection(camera_3d); + + //視点設定(0,0,0から、Y軸を180度回転してZ+方向を向くようにする。) + TransformGroup viewGroup = new TransformGroup(); + Transform3D viewTransform = new Transform3D(); + viewTransform.rotY(Math.PI); + viewTransform.setTranslation(new Vector3d(0.0, 0.0, 0.0)); + viewGroup.setTransform(viewTransform); + viewGroup.addChild(viewPlatform); + BranchGroup viewRoot = new BranchGroup(); + viewRoot.addChild(viewGroup); + locale.addBranchGraph(viewRoot); + + //バックグラウンドの作成 + Background background = new Background(); + BoundingSphere bounds = new BoundingSphere(); + bounds.setRadius(10.0); + background.setApplicationBounds(bounds); + background.setImageScaleMode(Background.SCALE_FIT_ALL); + background.setCapability(Background.ALLOW_IMAGE_WRITE); + BranchGroup root = new BranchGroup(); + root.addChild(background); + + //TransformGroupで囲ったシーングラフの作成 + TransformGroup transform = new TransformGroup(); + transform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); + transform.addChild(createSceneGraph()); + root.addChild(transform); + + //NyARToolkitのBehaviorを作る。(マーカーサイズはメートルで指定すること) + nya_behavior = new NyARSingleMarkerBehaviorHolder(ar_param, 30f, ar_code, 0.08); + //Behaviorに連動するグループをセット + nya_behavior.setTransformGroup(transform); + nya_behavior.setBackGround(background); + + //出来たbehaviorをセット + root.addChild(nya_behavior.getBehavior()); + nya_behavior.setUpdateListener(this); + + //表示ブランチをLocateにセット + locale.addBranchGraph(root); + + //ウインドウの設定 + setLayout(new BorderLayout()); + add(canvas, BorderLayout.CENTER); + } + + /** + * シーングラフを作って、そのノードを返す。 + * このノードは40mmの色つき立方体を表示するシーン。z軸を基準に20mm上に浮かせてる。 + * @return + */ + private Node createSceneGraph() + { + TransformGroup tg = new TransformGroup(); + Transform3D mt = new Transform3D(); + mt.setTranslation(new Vector3d(0.00, 0.0, 20 * 0.001)); + // 大きさ 40mmの色付き立方体を、Z軸上で20mm動かして配置) + tg.setTransform(mt); + tg.addChild(new ColorCube(20 * 0.001)); + return tg; + } +} diff --git a/tags/2.0.0/sample/jmf/.classpath b/tags/2.0.0/sample/jmf/.classpath new file mode 100644 index 0000000..6ba8b00 --- /dev/null +++ b/tags/2.0.0/sample/jmf/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/tags/2.0.0/sample/jmf/.project b/tags/2.0.0/sample/jmf/.project new file mode 100644 index 0000000..bdf574b --- /dev/null +++ b/tags/2.0.0/sample/jmf/.project @@ -0,0 +1,18 @@ + + + NyARToolkit.sample.jmf + + + NyARToolKit + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/sample/jmf/jp/nyatla/nyartoolkit/jmf/sample/NyarToolkitLinkTest.java b/tags/2.0.0/sample/jmf/jp/nyatla/nyartoolkit/jmf/sample/NyarToolkitLinkTest.java new file mode 100644 index 0000000..6a1984a --- /dev/null +++ b/tags/2.0.0/sample/jmf/jp/nyatla/nyartoolkit/jmf/sample/NyarToolkitLinkTest.java @@ -0,0 +1,145 @@ +/* + * PROJECT: NyARToolkit JMF sample program. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.jmf.sample; + +import javax.media.*; + +import javax.media.util.BufferToImage; +import javax.media.format.*; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.jmf.utils.*; + +import java.awt.*; + +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.transmat.*; +import jp.nyatla.nyartoolkit.detector.NyARSingleDetectMarker; + +/** + * VFM+ARToolkitテストプログラム + * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。 + */ +public class NyarToolkitLinkTest extends Frame implements JmfCaptureListener +{ + private final String CARCODE_FILE = "../../Data/patt.hiro"; + + private final String PARAM_FILE = "../../Data/camera_para.dat"; + + private JmfCameraCapture capture; + + private NyARSingleDetectMarker nya; + + private JmfNyARRaster_RGB raster; + + private NyARTransMatResult trans_mat_result = new NyARTransMatResult(); + + public NyarToolkitLinkTest() throws NyARException, NyARException + { + setTitle("JmfCaptureTest"); + setBounds(0, 0, 320 + 64, 240 + 64); + //キャプチャの準備 + capture = new JmfCameraCapture(320, 240, 30f, JmfCameraCapture.PIXEL_FORMAT_RGB); + capture.setCaptureListener(this); + + //NyARToolkitの準備 + NyARParam ar_param = new NyARParam(); + NyARCode ar_code = new NyARCode(16, 16); + ar_param.loadARParamFromFile(PARAM_FILE); + ar_param.changeScreenSize(320, 240); + nya = new NyARSingleDetectMarker(ar_param, ar_code, 80.0); + ar_code.loadARPattFromFile(CARCODE_FILE); + //キャプチャイメージ用のラスタを準備 + raster = new JmfNyARRaster_RGB(320, 240); + } + + public void onUpdateBuffer(Buffer i_buffer) + { + try { + //キャプチャしたバッファをラスタにセット + raster.setBuffer(i_buffer); + + //キャプチャしたイメージを表示用に加工 + BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat()); + Image img = b2i.createImage(i_buffer); + + Graphics g = getGraphics(); + + //マーカー検出 + boolean is_marker_exist = nya.detectMarkerLite(raster, 100); + if (is_marker_exist) { + //変換行列を取得 + nya.getTransmationMatrix(this.trans_mat_result); + } + //情報を画面に書く + g.drawImage(img, 32, 32, this); + if (is_marker_exist) { + g.drawString("マーカー検出:" + nya.getConfidence(), 32, 50); + g.drawString("[m00]" +this.trans_mat_result.m00, 32, 50 + 16*1); + g.drawString("[m01]" +this.trans_mat_result.m01, 32, 50 + 16*2); + g.drawString("[m02]" +this.trans_mat_result.m02, 32, 50 + 16*3); + g.drawString("[m03]" +this.trans_mat_result.m03, 32, 50 + 16*4); + g.drawString("[m10]" +this.trans_mat_result.m10, 32, 50 + 16*5); + g.drawString("[m11]" +this.trans_mat_result.m11, 32, 50 + 16*6); + g.drawString("[m12]" +this.trans_mat_result.m12, 32, 50 + 16*7); + g.drawString("[m13]" +this.trans_mat_result.m13, 32, 50 + 16*8); + g.drawString("[m20]" +this.trans_mat_result.m20, 32, 50 + 16*9); + g.drawString("[m21]" +this.trans_mat_result.m21, 32, 50 + 16*10); + g.drawString("[m22]" +this.trans_mat_result.m22, 32, 50 + 16*11); + g.drawString("[m23]" +this.trans_mat_result.m23, 32, 50 + 16*12); + } else { + g.drawString("マーカー未検出:", 32, 100); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + + private void startCapture() + { + try { + capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) + { + try { + NyarToolkitLinkTest mainwin = new NyarToolkitLinkTest(); + mainwin.setVisible(true); + mainwin.startCapture(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/tags/2.0.0/sample/jogl/.classpath b/tags/2.0.0/sample/jogl/.classpath new file mode 100644 index 0000000..f2d5180 --- /dev/null +++ b/tags/2.0.0/sample/jogl/.classpath @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/tags/2.0.0/sample/jogl/.project b/tags/2.0.0/sample/jogl/.project new file mode 100644 index 0000000..c36eee5 --- /dev/null +++ b/tags/2.0.0/sample/jogl/.project @@ -0,0 +1,17 @@ + + + NyARToolkit.sample.jogl + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite.java b/tags/2.0.0/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite.java new file mode 100644 index 0000000..970c0ec --- /dev/null +++ b/tags/2.0.0/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite.java @@ -0,0 +1,242 @@ +/* + * PROJECT: NyARToolkit JOGL sample program. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.jogl.sample; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.*; + +import javax.media.Buffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLCanvas; + +import com.sun.opengl.util.Animator; + +import jp.nyatla.nyartoolkit.core.NyARCode; + +import jp.nyatla.nyartoolkit.jmf.utils.JmfCameraCapture; +import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener; +import jp.nyatla.nyartoolkit.jogl.utils.*; +/** + * simpleLiteと同じようなテストプログラム + * 出来る限りARToolKitのサンプルと似せて作ってあります。 + * 最も一致する"Hiro"マーカーを一つ選択して、その上に立方体を表示します。 + * + */ +public class JavaSimpleLite implements GLEventListener, JmfCaptureListener +{ + private final String CARCODE_FILE = "../../Data/patt.hiro"; + + private final String PARAM_FILE = "../../Data/camera_para.dat"; + + private final static int SCREEN_X = 320; + + private final static int SCREEN_Y = 240; + + private Animator _animator; + + private GLNyARRaster_RGB _cap_image; + + private JmfCameraCapture _capture; + + private GL _gl; + + private NyARGLUtil _glnya; + + //NyARToolkit関係 + private GLNyARSingleDetectMarker _nya; + + private GLNyARParam _ar_param; + + /** + * 立方体を書く + * + */ + void drawCube() + { + // Colour cube data. + int polyList = 0; + float fSize = 0.5f;//マーカーサイズに対して0.5倍なので、4cmの立方体 + int f, i; + float[][] cube_vertices = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, -1.0f, 1.0f }, { -1.0f, -1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, -1.0f }, { 1.0f, -1.0f, -1.0f }, { -1.0f, -1.0f, -1.0f }, { -1.0f, 1.0f, -1.0f } }; + float[][] cube_vertex_colors = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } }; + int cube_num_faces = 6; + short[][] cube_faces = new short[][] { { 3, 2, 1, 0 }, { 2, 3, 7, 6 }, { 0, 1, 5, 4 }, { 3, 0, 4, 7 }, { 1, 2, 6, 5 }, { 4, 5, 6, 7 } }; + + if (polyList == 0) { + polyList = _gl.glGenLists(1); + _gl.glNewList(polyList, GL.GL_COMPILE); + _gl.glBegin(GL.GL_QUADS); + for (f = 0; f < cube_num_faces; f++) + for (i = 0; i < 4; i++) { + _gl.glColor3f(cube_vertex_colors[cube_faces[f][i]][0], cube_vertex_colors[cube_faces[f][i]][1], cube_vertex_colors[cube_faces[f][i]][2]); + _gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize); + } + _gl.glEnd(); + _gl.glColor3f(0.0f, 0.0f, 0.0f); + for (f = 0; f < cube_num_faces; f++) { + _gl.glBegin(GL.GL_LINE_LOOP); + for (i = 0; i < 4; i++) + _gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize); + _gl.glEnd(); + } + _gl.glEndList(); + } + + _gl.glPushMatrix(); // Save world coordinate system. + _gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface. + _gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis. + _gl.glDisable(GL.GL_LIGHTING); // Just use colours. + _gl.glCallList(polyList); // Draw the cube. + _gl.glPopMatrix(); // Restore world coordinate system. + + } + + public JavaSimpleLite() + { + Frame frame = new Frame("Java simpleLite with NyARToolkit"); + + // 3Dを描画するコンポーネント + GLCanvas canvas = new GLCanvas(); + frame.add(canvas); + canvas.addGLEventListener(this); + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) + { + System.exit(0); + } + }); + + frame.setVisible(true); + Insets ins = frame.getInsets(); + frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom); + canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y); + } + + public void init(GLAutoDrawable drawable) + { + _gl = drawable.getGL(); + _gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + //NyARToolkitの準備 + try { + //キャプチャの準備 + _capture = new JmfCameraCapture(SCREEN_X, SCREEN_Y, 15f, JmfCameraCapture.PIXEL_FORMAT_RGB); + _capture.setCaptureListener(this); + //NyARToolkitの準備 + _ar_param = new GLNyARParam(); + NyARCode ar_code = new NyARCode(16, 16); + _ar_param.loadARParamFromFile(PARAM_FILE); + _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y); + _nya = new GLNyARSingleDetectMarker(_ar_param, ar_code, 80.0); + _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。 + ar_code.loadARPattFromFile(CARCODE_FILE); + //NyARToolkit用の支援クラス + _glnya = new NyARGLUtil(_gl); + //GL対応のRGBラスタオブジェクト + _cap_image = new GLNyARRaster_RGB(_ar_param); + //キャプチャ開始 + _capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + _animator = new Animator(drawable); + + _animator.start(); + + } + + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) + { + _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + _gl.glViewport(0, 0, width, height); + + //視体積の設定 + _gl.glMatrixMode(GL.GL_PROJECTION); + _gl.glLoadIdentity(); + //見る位置 + _gl.glMatrixMode(GL.GL_MODELVIEW); + _gl.glLoadIdentity(); + } + + public void display(GLAutoDrawable drawable) + { + + try { + if (!_cap_image.hasData()) { + return; + } + _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame. + //画像チェックしてマーカー探して、背景を書く + boolean is_marker_exist; + synchronized (_cap_image) { + is_marker_exist = _nya.detectMarkerLite(_cap_image, 110); + //背景を書く + _glnya.drawBackGround(_cap_image, 1.0); + } + //あったら立方体を書く + if (is_marker_exist) { + //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。 + // Projection transformation. + _gl.glMatrixMode(GL.GL_PROJECTION); + _gl.glLoadMatrixd(_ar_param.getCameraFrustumRH(), 0); + _gl.glMatrixMode(GL.GL_MODELVIEW); + // Viewing transformation. + _gl.glLoadIdentity(); + _gl.glLoadMatrixd(_nya.getCameraViewRH(), 0); + + // All other lighting and geometry goes here. + drawCube(); + } + Thread.sleep(1);//タスク実行権限を一旦渡す + } catch (Exception e) { + e.printStackTrace(); + } + + } + public void onUpdateBuffer(Buffer i_buffer) + { + try { + synchronized (_cap_image) { + _cap_image.setBuffer(i_buffer, true); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) + { + } + + public static void main(String[] args) + { + new JavaSimpleLite(); + } +} diff --git a/tags/2.0.0/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite2.java b/tags/2.0.0/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite2.java new file mode 100644 index 0000000..8748b45 --- /dev/null +++ b/tags/2.0.0/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite2.java @@ -0,0 +1,256 @@ +/* + * PROJECT: NyARToolkit JOGL sample program. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package jp.nyatla.nyartoolkit.jogl.sample; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.*; + +import javax.media.Buffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLCanvas; + +import com.sun.opengl.util.Animator; + +import jp.nyatla.nyartoolkit.core.NyARCode; + +import jp.nyatla.nyartoolkit.jmf.utils.JmfCameraCapture; +import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener; +import jp.nyatla.nyartoolkit.jogl.utils.*; +/** + * simpleLiteの複数マーカー同時認識バージョン + * "Hiro"のマーカーと"人"のマーカーの混在環境で、Hiroのマーカー全てに + * 立方体を表示します。 + */ +public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener +{ + private final String CARCODE_FILE1 = "../../Data/patt.hiro"; + + private final String CARCODE_FILE2 = "../../Data/patt.kanji"; + + private final String PARAM_FILE = "../../Data/camera_para.dat"; + + private final static int SCREEN_X = 640; + + private final static int SCREEN_Y = 480; + + private Animator _animator; + + private GLNyARRaster_RGB _cap_image; + + private JmfCameraCapture _capture; + + private GL _gl; + + private NyARGLUtil _glnya; + + //NyARToolkit関係 + private GLNyARDetectMarker _nya; + + private GLNyARParam _ar_param; + + /** + * 立方体を書く + * + */ + void drawCube() + { + // Colour cube data. + int polyList = 0; + float fSize = 0.5f;//マーカーサイズに対して0.5倍なので、4cmの立方体 + int f, i; + float[][] cube_vertices = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, -1.0f, 1.0f }, { -1.0f, -1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, -1.0f }, { 1.0f, -1.0f, -1.0f }, { -1.0f, -1.0f, -1.0f }, { -1.0f, 1.0f, -1.0f } }; + float[][] cube_vertex_colors = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } }; + int cube_num_faces = 6; + short[][] cube_faces = new short[][] { { 3, 2, 1, 0 }, { 2, 3, 7, 6 }, { 0, 1, 5, 4 }, { 3, 0, 4, 7 }, { 1, 2, 6, 5 }, { 4, 5, 6, 7 } }; + + if (polyList == 0) { + polyList = _gl.glGenLists(1); + _gl.glNewList(polyList, GL.GL_COMPILE); + _gl.glBegin(GL.GL_QUADS); + for (f = 0; f < cube_num_faces; f++) + for (i = 0; i < 4; i++) { + _gl.glColor3f(cube_vertex_colors[cube_faces[f][i]][0], cube_vertex_colors[cube_faces[f][i]][1], cube_vertex_colors[cube_faces[f][i]][2]); + _gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize); + } + _gl.glEnd(); + _gl.glColor3f(0.0f, 0.0f, 0.0f); + for (f = 0; f < cube_num_faces; f++) { + _gl.glBegin(GL.GL_LINE_LOOP); + for (i = 0; i < 4; i++) + _gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize); + _gl.glEnd(); + } + _gl.glEndList(); + } + + _gl.glPushMatrix(); // Save world coordinate system. + _gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface. + _gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis. + _gl.glDisable(GL.GL_LIGHTING); // Just use colours. + _gl.glCallList(polyList); // Draw the cube. + _gl.glPopMatrix(); // Restore world coordinate system. + + } + + public JavaSimpleLite2() + { + Frame frame = new Frame("Java simpleLite with NyARToolkit"); + + // 3Dを描画するコンポーネント + GLCanvas canvas = new GLCanvas(); + frame.add(canvas); + canvas.addGLEventListener(this); + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) + { + System.exit(0); + } + }); + + frame.setVisible(true); + Insets ins = frame.getInsets(); + frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom); + canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y); + } + + public void init(GLAutoDrawable drawable) + { + _gl = drawable.getGL(); + _gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + //NyARToolkitの準備 + try { + //キャプチャの準備 + _capture = new JmfCameraCapture(SCREEN_X, SCREEN_Y, 15f, JmfCameraCapture.PIXEL_FORMAT_RGB); + _capture.setCaptureListener(this); + //NyARToolkitの準備 + _ar_param = new GLNyARParam(); + _ar_param.loadARParamFromFile(PARAM_FILE); + _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y); + + //ARコードを2個ロード + double[] width = new double[] { 80.0, 80.0 }; + NyARCode[] ar_codes = new NyARCode[2]; + ar_codes[0] = new NyARCode(16, 16); + ar_codes[0].loadARPattFromFile(CARCODE_FILE1); + ar_codes[1] = new NyARCode(16, 16); + ar_codes[1].loadARPattFromFile(CARCODE_FILE2); + _nya = new GLNyARDetectMarker(_ar_param, ar_codes, width, 2); + _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。 + //NyARToolkit用の支援クラス + _glnya = new NyARGLUtil(_gl); + //GL対応のRGBラスタオブジェクト + _cap_image = new GLNyARRaster_RGB(_ar_param); + //キャプチャ開始 + _capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + _animator = new Animator(drawable); + + _animator.start(); + + } + + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) + { + _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + _gl.glViewport(0, 0, width, height); + + //視体積の設定 + _gl.glMatrixMode(GL.GL_PROJECTION); + _gl.glLoadIdentity(); + //見る位置 + _gl.glMatrixMode(GL.GL_MODELVIEW); + _gl.glLoadIdentity(); + } + + public void display(GLAutoDrawable drawable) + { + + try { + if (!_cap_image.hasData()) { + return; + } + _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame. + //画像チェックしてマーカー探して、背景を書く + int found_markers; + synchronized (_cap_image) { + found_markers = _nya.detectMarkerLite(_cap_image, 110); + //背景を書く + _glnya.drawBackGround(_cap_image, 1.0); + } + //あったら立方体を書く + double[] matrix = new double[16]; + for (int i = 0; i < found_markers; i++) { + //1番のマーカーでなければ表示しない。 + if (_nya.getARCodeIndex(i) != 0) { + continue; + } + //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。 + // Projection transformation. + _gl.glMatrixMode(GL.GL_PROJECTION); + _gl.glLoadMatrixd(_ar_param.getCameraFrustumRH(), 0); + _gl.glMatrixMode(GL.GL_MODELVIEW); + // Viewing transformation. + _gl.glLoadIdentity(); + _nya.getCameraViewRH(i, matrix); + _gl.glLoadMatrixd(matrix, 0); + + // All other lighting and geometry goes here. + drawCube(); + } + Thread.sleep(1);//タスク実行権限を一旦渡す + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void onUpdateBuffer(Buffer i_buffer) + { + try { + synchronized (_cap_image) { + _cap_image.setBuffer(i_buffer, true); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) + { + } + + public static void main(String[] args) + { + new JavaSimpleLite2(); + } +} diff --git a/tags/2.0.0/sample/qt/.classpath b/tags/2.0.0/sample/qt/.classpath new file mode 100644 index 0000000..2808887 --- /dev/null +++ b/tags/2.0.0/sample/qt/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/tags/2.0.0/sample/qt/.project b/tags/2.0.0/sample/qt/.project new file mode 100644 index 0000000..d30a0bd --- /dev/null +++ b/tags/2.0.0/sample/qt/.project @@ -0,0 +1,19 @@ + + + NyARToolkit.sample.qt + + + NyARToolKit + NyARToolkit.utils.qt + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/sample/qt/jp/nyatla/nyartoolkit/qt/sample/NyarToolkitLinkTest.java b/tags/2.0.0/sample/qt/jp/nyatla/nyartoolkit/qt/sample/NyarToolkitLinkTest.java new file mode 100644 index 0000000..79489c5 --- /dev/null +++ b/tags/2.0.0/sample/qt/jp/nyatla/nyartoolkit/qt/sample/NyarToolkitLinkTest.java @@ -0,0 +1,139 @@ +/* + * PROJECT: NyARToolkit QuickTime sample program. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + + +package jp.nyatla.nyartoolkit.qt.sample; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.qt.utils.*; +import java.awt.*; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; +import jp.nyatla.nyartoolkit.detector.NyARSingleDetectMarker; +/** + * VFM+ARToolkitテストプログラム + * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。 + */ +public class NyarToolkitLinkTest extends Frame implements QtCaptureListener +{ + private final String CARCODE_FILE = "../../Data/patt.hiro"; + + private final String PARAM_FILE = "../../Data/camera_para.dat"; + + private QtCameraCapture capture; + + private NyARSingleDetectMarker nya; + + private QtNyARRaster_RGB raster; + + private NyARTransMatResult trans_mat_result = new NyARTransMatResult(); + + public NyarToolkitLinkTest() throws NyARException, NyARException + { + setTitle("QtCaptureTest"); + setBounds(0, 0, 320 + 64, 240 + 64); + //キャプチャの準備 + capture = new QtCameraCapture(320, 240, 30f); + capture.setCaptureListener(this); + + //NyARToolkitの準備 + NyARParam ar_param = new NyARParam(); + NyARCode ar_code = new NyARCode(16, 16); + ar_param.loadARParamFromFile(PARAM_FILE); + ar_param.changeScreenSize(320, 240); + nya = new NyARSingleDetectMarker(ar_param, ar_code, 80.0); + ar_code.loadARPattFromFile(CARCODE_FILE); + //キャプチャイメージ用のラスタを準備 + raster = new QtNyARRaster_RGB(320, 240); + } + + public void onUpdateBuffer(byte[] pixels) + { + try { + //キャプチャしたバッファをラスタにセット + raster.setBuffer(pixels); + + //キャプチャしたイメージを表示用に加工 + Image img = raster.createImage(); + + Graphics g = getGraphics(); + + //マーカー検出 + boolean is_marker_exist = nya.detectMarkerLite(raster, 100); + if (is_marker_exist) { + //変換行列を取得 + nya.getTransmationMatrix(this.trans_mat_result); + } + //情報を画面に書く + g.drawImage(img, 32, 32, this); + if (is_marker_exist) { + g.drawString("マーカー検出:" + nya.getConfidence(), 32, 50); + g.drawString("[m00]" + this.trans_mat_result.m00, 32, 50 + 16 * 1); + g.drawString("[m01]" + this.trans_mat_result.m01, 32, 50 + 16 * 2); + g.drawString("[m02]" + this.trans_mat_result.m02, 32, 50 + 16 * 3); + g.drawString("[m03]" + this.trans_mat_result.m03, 32, 50 + 16 * 4); + g.drawString("[m10]" + this.trans_mat_result.m10, 32, 50 + 16 * 5); + g.drawString("[m11]" + this.trans_mat_result.m11, 32, 50 + 16 * 6); + g.drawString("[m12]" + this.trans_mat_result.m12, 32, 50 + 16 * 7); + g.drawString("[m13]" + this.trans_mat_result.m13, 32, 50 + 16 * 8); + g.drawString("[m20]" + this.trans_mat_result.m20, 32, 50 + 16 * 9); + g.drawString("[m21]" + this.trans_mat_result.m21, 32, 50 + 16 * 10); + g.drawString("[m22]" + this.trans_mat_result.m22, 32, 50 + 16 * 11); + g.drawString("[m23]" + this.trans_mat_result.m23, 32, 50 + 16 * 12); + + } else { + g.drawString("マーカー未検出:", 32, 100); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + + private void startCapture() + { + try { + capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) + { + try { + NyarToolkitLinkTest mainwin = new NyarToolkitLinkTest(); + mainwin.setVisible(true); + mainwin.startCapture(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/tags/2.0.0/sample/qt/jp/nyatla/nyartoolkit/qt/sample/QtCaptureTest.java b/tags/2.0.0/sample/qt/jp/nyatla/nyartoolkit/qt/sample/QtCaptureTest.java new file mode 100644 index 0000000..4631c23 --- /dev/null +++ b/tags/2.0.0/sample/qt/jp/nyatla/nyartoolkit/qt/sample/QtCaptureTest.java @@ -0,0 +1,82 @@ +/* + * PROJECT: NyARToolkit QuickTime sample program. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.qt.sample; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.qt.utils.*; + +import java.awt.*; +/** + * QuickTimeキャプチャプログラム + * + */ +public class QtCaptureTest extends Frame implements QtCaptureListener +{ + public QtCaptureTest() throws NyARException + { + setTitle("QtCaptureTest"); + setBounds(0, 0, 320 + 64, 240 + 64); + capture = new QtCameraCapture(320, 240, 30f); + capture.setCaptureListener(this); + //キャプチャイメージ用のラスタを準備 + raster = new QtNyARRaster_RGB(320, 240); + } + + private QtCameraCapture capture; + + private QtNyARRaster_RGB raster; + + public void onUpdateBuffer(byte[] pixels) + { + raster.setBuffer(pixels); + Image img = raster.createImage(); + Graphics g = getGraphics(); + g.drawImage(img, 32, 32, this); + } + + private void startCapture() + { + try { + capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) + { + try { + QtCaptureTest mainwin = new QtCaptureTest(); + mainwin.setVisible(true); + mainwin.startCapture(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/tags/2.0.0/src.utils/java3d/.classpath b/tags/2.0.0/src.utils/java3d/.classpath new file mode 100644 index 0000000..d15273a --- /dev/null +++ b/tags/2.0.0/src.utils/java3d/.classpath @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/tags/2.0.0/src.utils/java3d/.project b/tags/2.0.0/src.utils/java3d/.project new file mode 100644 index 0000000..2bb7bf7 --- /dev/null +++ b/tags/2.0.0/src.utils/java3d/.project @@ -0,0 +1,17 @@ + + + NyARToolkit.utils.java3d + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/J3dNyARParam.java b/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/J3dNyARParam.java new file mode 100644 index 0000000..266634d --- /dev/null +++ b/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/J3dNyARParam.java @@ -0,0 +1,141 @@ +/* + * PROJECT: NyARToolkit Java3D utilities. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.java3d.utils; + +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; + +import javax.media.j3d.Transform3D; +/** + * NyARParamにJava3D向け関数を追加したもの + */ +public class J3dNyARParam extends NyARParam +{ + private double view_distance_min = 0.01;//1cm~10.0m + + private double view_distance_max = 10.0; + + private Transform3D m_projection = null; + + /** + * 視体積の近い方をメートルで指定 + * @param i_new_value + */ + public void setViewDistanceMin(double i_new_value) + { + m_projection = null;//キャッシュ済変数初期化 + view_distance_min = i_new_value; + } + + /** + * 視体積の遠い方をメートルで指定 + * @param i_new_value + */ + public void setViewDistanceMax(double i_new_value) + { + m_projection = null;//キャッシュ済変数初期化 + view_distance_max = i_new_value; + } + + /** + * void arglCameraFrustumRH(const ARParam *cparam, const double focalmin, const double focalmax, GLdouble m_projection[16]) + * 関数の置き換え + * @param focalmin + * @param focalmax + * @return + */ + public Transform3D getCameraTransform() + { + //既に値がキャッシュされていたらそれを使う + if (m_projection != null) { + return m_projection; + } + //無ければ計算 + + NyARMat trans_mat = new NyARMat(3, 4); + NyARMat icpara_mat = new NyARMat(3, 4); + double[][] p = new double[3][3], q = new double[4][4]; + double width, height; + int i, j; + + width = this._screen_size.w; + height = this._screen_size.h; + + this.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat); + + double[][] icpara = icpara_mat.getArray(); + double[][] trans = trans_mat.getArray(); + for (i = 0; i < 4; i++) { + icpara[1][i] = (height - 1) * (icpara[2][i]) - icpara[1][i]; + } + + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + p[i][j] = icpara[i][j] / icpara[2][2]; + } + } + //p[0][0],p[1][1]=n + //p[0][2],p[1][2]=t+b + + //Projectionの計算 + q[0][0] = (2.0 * p[0][0] / (width - 1)); + q[0][1] = (2.0 * p[0][1] / (width - 1)); + q[0][2] = -((2.0 * p[0][2] / (width - 1)) - 1.0); + q[0][3] = 0.0; + + q[1][0] = 0.0; + q[1][1] = -(2.0 * p[1][1] / (height - 1)); + q[1][2] = -((2.0 * p[1][2] / (height - 1)) - 1.0); + q[1][3] = 0.0; + + q[2][0] = 0.0; + q[2][1] = 0.0; + q[2][2] = (view_distance_max + view_distance_min) / (view_distance_min - view_distance_max); + q[2][3] = 2.0 * view_distance_max * view_distance_min / (view_distance_min - view_distance_max); + + q[3][0] = 0.0; + q[3][1] = 0.0; + q[3][2] = -1.0; + q[3][3] = 0.0; + + q[2][2] = q[2][2] * -1; + q[2][3] = q[2][3] * -1; + + double[] tmp_projection = new double[16]; + for (i = 0; i < 4; i++) { // Row. + // First 3 columns of the current row. + for (j = 0; j < 3; j++) { // Column. + tmp_projection[i + j * 4] = (q[i][0] * trans[0][j] + q[i][1] * trans[1][j] + q[i][2] * trans[2][j]); + } + // Fourth column of the current row. + tmp_projection[i + 3 * 4] = q[i][0] * trans[0][3] + q[i][1] * trans[1][3] + q[i][2] * trans[2][3] + q[i][3]; + } + m_projection = new Transform3D(tmp_projection); + m_projection.transpose(); + return m_projection; + } +} diff --git a/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/J3dNyARRaster_RGB.java b/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/J3dNyARRaster_RGB.java new file mode 100644 index 0000000..c793898 --- /dev/null +++ b/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/J3dNyARRaster_RGB.java @@ -0,0 +1,98 @@ +/* + * PROJECT: NyARToolkit Java3D utilities. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.java3d.utils; + +import java.awt.image.*; + +import javax.media.format.RGBFormat; +import javax.media.j3d.ImageComponent; +import javax.media.j3d.ImageComponent2D; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.jmf.utils.*; + +/** + * + * このクラスは、Java3Dと互換性のあるNyARToolkitのラスタイメージを保持します。 + * + */ +public class J3dNyARRaster_RGB extends JmfNyARRaster_RGB +{ + private ImageComponent2D imc2d; + + private byte[] i2d_buf; + + private BufferedImage bufferd_image; + + /** + * JMFのキャプチャ画像をこのクラスのBufferedImageにコピーします。 + * @param i_buffer + * 画像の格納されたバッファを指定して下さい。 + * 画像サイズはコンストラクタで与えたパラメタと同じサイズである必要があります。 + */ + public void setBuffer(javax.media.Buffer i_buffer) throws NyARException + { + //メモ:この時点では、ref_dataにはi_bufferの参照値が入ってる。 + synchronized (this) { + //キャプチャデータをi2dのバッファにコピーする。(これ省略したいなあ…。) + System.arraycopy((byte[]) i_buffer.getData(), 0, this.i2d_buf, 0, this.i2d_buf.length); + } + int buffer_type = analyzeBufferType((RGBFormat) i_buffer.getFormat()); + this._ref_buf = this.i2d_buf; + this._reader.changeBuffer(buffer_type, this._ref_buf); + } + + public J3dNyARRaster_RGB(NyARParam i_cparam) + { + super(i_cparam.getScreenSize()); + + //RGBのラスタを作る。 + this.bufferd_image = new BufferedImage(this._size.w, this._size.h, BufferedImage.TYPE_3BYTE_BGR); + i2d_buf = ((DataBufferByte) bufferd_image.getRaster().getDataBuffer()).getData(); + this.imc2d = new ImageComponent2D(ImageComponent2D.FORMAT_RGB, this.bufferd_image, true, true); + imc2d.setCapability(ImageComponent.ALLOW_IMAGE_WRITE); + } + + /** + * 自身の格納しているImageComponent2Dオブジェクトを作り直します。 + * Java3D1.5がDirectXで動いた(らしいとき)に、ImageComponent2Dのインスタンス + * IDが異ならないと、Behavior内でイメージの更新を通知できない事象に対応するために実装してあります。 + * Behavior内でgetImageComponent2()関数を実行する直前に呼び出すことで、この事象を回避することができます。 + * + */ + public void renewImageComponent2D() + { + this.imc2d = new ImageComponent2D(ImageComponent2D.FORMAT_RGB, this.bufferd_image, true, true); + this.imc2d.setCapability(ImageComponent.ALLOW_IMAGE_WRITE); + } + + public ImageComponent2D getImageComponent2D() + { + return this.imc2d; + } +} diff --git a/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/NyARSingleMarkerBehaviorHolder.java b/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/NyARSingleMarkerBehaviorHolder.java new file mode 100644 index 0000000..fb76546 --- /dev/null +++ b/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/NyARSingleMarkerBehaviorHolder.java @@ -0,0 +1,232 @@ +/* + * PROJECT: NyARToolkit Java3D utilities. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.java3d.utils; + +import java.util.Enumeration; + +import javax.media.Buffer; +import javax.media.j3d.*; +import javax.vecmath.*; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.jmf.utils.JmfCameraCapture; +import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; +import jp.nyatla.nyartoolkit.detector.*; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * NyARToolkitと連動したBehaviorを返却するクラスです。 + * 提供できるBehaviorは、BackgroundとTransformgroupです。 + * + */ +public class NyARSingleMarkerBehaviorHolder implements JmfCaptureListener +{ + private NyARParam cparam; + + private JmfCameraCapture capture; + + private J3dNyARRaster_RGB nya_raster;//最大3スレッドで共有されるので、排他制御かけること。 + + private NyARSingleDetectMarker nya; + + //Behaviorホルダ + private NyARBehavior nya_behavior; + + public NyARSingleMarkerBehaviorHolder(NyARParam i_cparam, float i_rate, NyARCode i_ar_code, double i_marker_width) throws NyARException + { + nya_behavior = null; + final NyARIntSize scr_size = i_cparam.getScreenSize(); + cparam = i_cparam; + capture = new JmfCameraCapture(scr_size.w, scr_size.h, i_rate, JmfCameraCapture.PIXEL_FORMAT_RGB); + capture.setCaptureListener(this); + nya_raster = new J3dNyARRaster_RGB(cparam); + nya = new NyARSingleDetectMarker(cparam, i_ar_code, i_marker_width); + nya_behavior = new NyARBehavior(nya, nya_raster, i_rate); + } + + public Behavior getBehavior() + { + return nya_behavior; + } + + /** + * i_back_groundにキャプチャ画像を転送するようにBehaviorを設定します。 + * i_back_groungはALLOW_IMAGE_WRITE属性を持つものである必要があります。 + * @param i_back_groung + * @return + */ + public void setBackGround(Background i_back_ground) + { + //コール先で排他制御 + nya_behavior.setRelatedBackGround(i_back_ground); + } + + /** + * i_trgroupの座標系をマーカーにあわせるようにBehaviorを設定します。 + * + */ + public void setTransformGroup(TransformGroup i_trgroup) + { + //コール先で排他制御 + nya_behavior.setRelatedTransformGroup(i_trgroup); + } + + /** + * 座標系再計算後に呼び出されるリスナです。 + * @param i_listener + */ + public void setUpdateListener(NyARSingleMarkerBehaviorListener i_listener) + { + //コール先で排他制御 + nya_behavior.setUpdateListener(i_listener); + } + + /** + * ラスタを更新 コールバック関数だから呼んじゃらめえ + */ + public void onUpdateBuffer(Buffer i_buffer) + { + try { + synchronized (nya_raster) { + nya_raster.setBuffer(i_buffer); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void start() throws NyARException + { + //開始 + capture.start(); + } + + public void stop() + { + capture.stop(); + } +} + +class NyARBehavior extends Behavior +{ + private NyARTransMatResult trans_mat_result = new NyARTransMatResult(); + + private NyARSingleDetectMarker related_nya; + + private TransformGroup trgroup; + + private Background back_ground; + + private J3dNyARRaster_RGB raster; + + private WakeupCondition wakeup; + + private NyARSingleMarkerBehaviorListener listener; + + public void initialize() + { + wakeupOn(wakeup); + } + + /** + * i_related_ic2dの内容で定期的にi_back_groundを更新するBehavior + * @param i_back_ground + * @param i_related_ic2d + */ + public NyARBehavior(NyARSingleDetectMarker i_related_nya, J3dNyARRaster_RGB i_related_raster, float i_rate) + { + super(); + wakeup = new WakeupOnElapsedTime((int) (1000 / i_rate)); + related_nya = i_related_nya; + trgroup = null; + raster = i_related_raster; + back_ground = null; + listener = null; + this.setSchedulingBounds(new BoundingSphere(new Point3d(), 100.0)); + } + + public void setRelatedBackGround(Background i_back_ground) + { + synchronized (raster) { + back_ground = i_back_ground; + } + } + + public void setRelatedTransformGroup(TransformGroup i_trgroup) + { + synchronized (raster) { + trgroup = i_trgroup; + } + } + + public void setUpdateListener(NyARSingleMarkerBehaviorListener i_listener) + { + synchronized (raster) { + listener = i_listener; + } + } + + /** + * いわゆるイベントハンドラ + */ + public void processStimulus(Enumeration criteria) + { + try { + synchronized (raster) { + Transform3D t3d = null; + boolean is_marker_exist = false; + if (back_ground != null) { + raster.renewImageComponent2D();/*DirectXモードのときの対策*/ + back_ground.setImage(raster.getImageComponent2D()); + } + if (raster.hasData()) { + is_marker_exist = related_nya.detectMarkerLite(raster, 100); + if (is_marker_exist) + { + final NyARTransMatResult src = this.trans_mat_result; + related_nya.getTransmationMatrix(src); + Matrix4d matrix = new Matrix4d(src.m00, -src.m10, -src.m20, 0, -src.m01, src.m11, src.m21, 0, -src.m02, src.m12, src.m22, 0, -src.m03, src.m13, src.m23, 1); + matrix.transpose(); + t3d = new Transform3D(matrix); + if (trgroup != null) { + trgroup.setTransform(t3d); + } + } + } + if (listener != null) { + listener.onUpdate(is_marker_exist, t3d); + } + } + wakeupOn(wakeup); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/NyARSingleMarkerBehaviorListener.java b/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/NyARSingleMarkerBehaviorListener.java new file mode 100644 index 0000000..14654cc --- /dev/null +++ b/tags/2.0.0/src.utils/java3d/jp/nyatla/nyartoolkit/java3d/utils/NyARSingleMarkerBehaviorListener.java @@ -0,0 +1,48 @@ +/* + * PROJECT: NyARToolkit Java3D utilities. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.java3d.utils; + +import javax.media.j3d.*; +/** + * + * NyARToolkitのBehaviorのリスナ + * + */ +public interface NyARSingleMarkerBehaviorListener +{ + /** + * このリスナは、リスナにマーカーに連動してオブジェクトを操作するチャンスを与えます。 + * リスナはNyARSingleMarkerBehavior関数内のprocessStimulus関数から呼び出されます。 + * + * @param i_is_marker_exist + * マーカーが存在する場合true、存在しない場合、falseです。 + * @param i_transform3d + * マーカーが存在する場合、その変換行列が指定されます。 + * i_is_marker_existがtrueの時だけ有効です。 + */ + public void onUpdate(boolean i_is_marker_exist,Transform3D i_transform3d); +} diff --git a/tags/2.0.0/src.utils/jmf/.classpath b/tags/2.0.0/src.utils/jmf/.classpath new file mode 100644 index 0000000..68860c8 --- /dev/null +++ b/tags/2.0.0/src.utils/jmf/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tags/2.0.0/src.utils/jmf/.project b/tags/2.0.0/src.utils/jmf/.project new file mode 100644 index 0000000..12236e7 --- /dev/null +++ b/tags/2.0.0/src.utils/jmf/.project @@ -0,0 +1,18 @@ + + + NyARToolkit.utils.jmf + + + NyARToolKit + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfCameraCapture.java b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfCameraCapture.java new file mode 100644 index 0000000..8f41946 --- /dev/null +++ b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfCameraCapture.java @@ -0,0 +1,214 @@ +/* + * PROJECT: NyARToolkit JMF utilities. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.jmf.utils; + +import javax.media.*; +import javax.media.protocol.*; +import javax.media.control.*; +import javax.media.format.*; +import java.awt.*; +import java.util.*; +import javax.media.protocol.DataSource; + +import jp.nyatla.nyartoolkit.NyARException; +/** + * 簡易JMFキャプチャクラス + * @author atla + * + */ +public class JmfCameraCapture +{ + private Dimension image_size; + + private JmfCaptureListener capture_listener; + + // private DataSource jmf_data_source; + private MonitorStream jmf_monitor_stream; + + private Processor jmf_processor; + + private VideoFormat jmf_video_format; + + private Buffer read_buf = new Buffer(); + + public static final String PIXEL_FORMAT_RGB = "RGB"; + + public JmfCameraCapture(int i_width, int i_height, float i_rate, String i_pixcel_format) + { + String encoding = i_pixcel_format;// comboEncoding.getSelectedItem(); + image_size = new Dimension(i_width, i_height); + jmf_video_format = new VideoFormat(encoding, image_size, Format.NOT_SPECIFIED, null, i_rate); + } + + public Dimension getSize() + { + return image_size; + } + + public javax.media.Buffer readBuffer() throws NyARException + { + if (jmf_monitor_stream == null) { + throw new NyARException(); + } + try { + jmf_monitor_stream.read(read_buf); + } catch (Exception e) { + throw new NyARException(e); + } + return read_buf; + } + + public void setCaptureListener(JmfCaptureListener i_listener) throws NyARException + { + if (jmf_processor != null) { + throw new NyARException(); + } + capture_listener = i_listener; + + } + + public void start() throws NyARException + { + + DataSource ds = getCaptureDS(jmf_video_format); + VideoFormat[] formats = new VideoFormat[] { new VideoFormat(null) }; + ProcessorModel pm = new ProcessorModel(ds, formats, null);// , + // formats, + // ftd); + Processor processor; + try { + processor = Manager.createRealizedProcessor(pm); + } catch (Exception e) { + // Make sure the capture devices are released + ds.disconnect(); + throw new NyARException(e); + } + // Get the monitor control: + // Since there are more than one MonitorControl objects + // exported by the DataSource, we get the specific one + // that is also the MonitorStream object. + jmf_monitor_stream = (MonitorStream) ds.getControl("jmfsample.MonitorStream"); + jmf_monitor_stream.setCaptureListener(capture_listener); + // jmf_data_source=ds; + jmf_processor = processor; + jmf_processor.start(); + } + + public void stop() + { + jmf_processor.stop(); + jmf_processor.close(); + jmf_processor = null; + + } + + protected void finalize() + { + if (jmf_processor != null) { + jmf_processor.stop(); + jmf_processor.close(); + jmf_processor = null; + } + } + + private static DataSource getCaptureDS(VideoFormat vf) + { + DataSource dsVideo = null; + DataSource ds = null; + + // Create a capture DataSource for the video + // If there is no video capture device, then exit with null + if (vf != null) { + dsVideo = createDataSource(vf); + if (dsVideo == null) + return null; + } + + // Create the monitoring datasource wrapper + if (dsVideo != null) { + dsVideo = new MonitorCDS(dsVideo); + return dsVideo; + } + + // Merge the data sources, if both audio and video are available + try { + ds = Manager.createMergingDataSource(new DataSource[] { dsVideo }); + } catch (IncompatibleSourceException ise) { + return null; + } + + return ds; + } + + private static DataSource createDataSource(Format format) + { + DataSource ds; + Vector devices; + CaptureDeviceInfo cdi; + MediaLocator ml; + + // Find devices for format + devices = CaptureDeviceManager.getDeviceList(format); + if (devices.size() < 1) { + System.err.println("! No Devices for " + format); + return null; + } + // Pick the first device + cdi = (CaptureDeviceInfo) devices.elementAt(0); + + ml = cdi.getLocator(); + + try { + ds = Manager.createDataSource(ml); + ds.connect(); + if (ds instanceof CaptureDevice) { + setCaptureFormat((CaptureDevice) ds, format); + } + } catch (Exception e) { + System.err.println(e); + return null; + } + return ds; + } + + private static void setCaptureFormat(CaptureDevice cdev, Format format) + { + FormatControl[] fcs = cdev.getFormatControls(); + if (fcs.length < 1) { + return; + } + FormatControl fc = fcs[0]; + Format[] formats = fc.getSupportedFormats(); + for (int i = 0; i < formats.length; i++) { + if (formats[i].matches(format)) { + format = formats[i].intersects(format); + fc.setFormat(format); + break; + } + } + } +} \ No newline at end of file diff --git a/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfCaptureListener.java b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfCaptureListener.java new file mode 100644 index 0000000..2e9ff02 --- /dev/null +++ b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfCaptureListener.java @@ -0,0 +1,37 @@ +/* + * PROJECT: NyARToolkit JMF utilities. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.jmf.utils; + +import javax.media.Buffer; +/** + * JMF簡易キャプチャ用のリスナクラス + * + */ +public interface JmfCaptureListener{ + public void onUpdateBuffer(Buffer i_buffer); + +} \ No newline at end of file diff --git a/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfNyARRaster_RGB.java b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfNyARRaster_RGB.java new file mode 100644 index 0000000..efdf0ef --- /dev/null +++ b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/JmfNyARRaster_RGB.java @@ -0,0 +1,213 @@ +/* + * PROJECT: NyARToolkit JMF utilities. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.jmf.utils; + +import javax.media.format.*; +import java.awt.Dimension; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; +import jp.nyatla.nyartoolkit.core.rasterreader.*; +import jp.nyatla.nyartoolkit.core.types.*; +/** + * + * RGB形式のJMFバッファをラップするNyARRasterです。 + * JMFから得たラスタデータのピクセル並び順を考慮します。 + * + */ +public class JmfNyARRaster_RGB extends NyARRgbRaster_BasicClass +{ + protected class Reader implements INyARRgbPixelReader,INyARBufferReader + { + private int _buffer_type = INyARBufferReader.BUFFERFORMAT_NULL_ALLZERO; + private byte[] _ref_buf; + private NyARIntSize _size; + + public Reader(NyARIntSize i_size) + { + this._size = i_size; + } + // + //INyARRgbPixelReader + // + public void getPixel(int i_x, int i_y, int[] o_rgb) throws NyARException + { + int bp = (i_x + i_y * this._size.w) * 3; + byte[] ref = this._ref_buf; + switch (this._buffer_type) { + case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24: + o_rgb[0] = (ref[bp + 0] & 0xff);// R + o_rgb[1] = (ref[bp + 1] & 0xff);// G + o_rgb[2] = (ref[bp + 2] & 0xff);// B + break; + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24: + o_rgb[0] = (ref[bp + 2] & 0xff);// B + o_rgb[1] = (ref[bp + 1] & 0xff);// G + o_rgb[2] = (ref[bp + 0] & 0xff);// R + break; + default: + throw new NyARException(); + } + return; + } + + public void getPixelSet(int[] i_x, int i_y[], int i_num, int[] o_rgb) throws NyARException + { + int width = this._size.w; + byte[] ref = this._ref_buf; + int bp; + switch (this._buffer_type) { + case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24: + for (int i = i_num - 1; i >= 0; i--) { + bp = (i_x[i] + i_y[i] * width) * 3; + o_rgb[i * 3 + 0] = (ref[bp + 0] & 0xff);// R + o_rgb[i * 3 + 1] = (ref[bp + 1] & 0xff);// G + o_rgb[i * 3 + 2] = (ref[bp + 2] & 0xff);// B + } + break; + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24: + for (int i = i_num - 1; i >= 0; i--) { + bp = (i_x[i] + i_y[i] * width) * 3; + o_rgb[i * 3 + 0] = (ref[bp + 2] & 0xff);// R + o_rgb[i * 3 + 1] = (ref[bp + 1] & 0xff);// G + o_rgb[i * 3 + 2] = (ref[bp + 0] & 0xff);// B + } + break; + default: + throw new NyARException(); + } + return; + } + + public void changeBuffer(int i_buffer_type, byte[] i_buffer) + { + if(i_buffer_type==1){ + System.out.println("aaa"); + } + + this._buffer_type = i_buffer_type; + this._ref_buf = i_buffer; + } + // + //INyARBufferReader + // + public Object getBuffer() + { + return this._ref_buf; + } + public int getBufferType() + { + return _buffer_type; + } + public boolean isEqualBufferType(int i_type_value) + { + return this._buffer_type==i_type_value; + } + } + + protected byte[] _ref_buf; + protected Reader _reader; + /** + * RGB形式のJMFバッファをラップするオブジェクトをつくります。 生成直後のオブジェクトはデータを持ちません。 + * メンバ関数はsetBufferを実行後に使用可能になります。 + */ + public JmfNyARRaster_RGB(NyARIntSize i_size) + { + super(new NyARIntSize(i_size.w,i_size.w)); + this._size.w = i_size.w; + this._size.h = i_size.h; + this._ref_buf = null; + this._reader = new Reader(this._size); + } + public JmfNyARRaster_RGB(int i_width,int i_height) + { + super(new NyARIntSize(i_width,i_height)); + this._ref_buf = null; + this._reader = new Reader(this._size); + } + + /** + * フォーマットを解析して、ラスタタイプを返します。 + * + * @param i_fmt + * @throws NyARException + */ + protected int analyzeBufferType(RGBFormat i_fmt) throws NyARException + { + // データサイズの確認 + Dimension s = i_fmt.getSize(); + if (!this._size.isEqualSize(s.width, s.height)) { + throw new NyARException(); + } + // データ配列の確認 + int r = i_fmt.getRedMask() - 1; + int b = i_fmt.getBlueMask() - 1; + + // 色配列の特定 + if (r == 0 && b == 2) { + return INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24; + } else if (r == 2 && b == 0) { + return INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24; + } else { + throw new NyARException("Unknown pixel order."); + } + } + + /** + * javax.media.Bufferを分析して、その分析結果をNyARRasterに適合する形で保持します。 + * 関数実行後に外部でi_bufferの内容変更した場合には、再度setBuffer関数を呼び出してください。 + * この関数を実行すると、getRgbPixelReaderで取得したReaderのプロパティが変化することがあります。 + * @param i_buffer + * RGB形式のデータを格納したjavax.media.Bufferオブジェクトを指定してください。 + * @return i_bufferをラップしたオブジェクトを返します。 + * @throws NyARException + */ + public void setBuffer(javax.media.Buffer i_buffer) throws NyARException + { + int buftype= analyzeBufferType((RGBFormat) i_buffer.getFormat()); + this._ref_buf = (byte[]) i_buffer.getData(); + this._reader.changeBuffer(buftype, this._ref_buf); + } + + /** + * データを持っているかを返します。 + * @return + */ + public boolean hasData() + { + return this._ref_buf != null; + } + + public INyARRgbPixelReader getRgbPixelReader() + { + return this._reader; + } + public INyARBufferReader getBufferReader() + { + return this._reader; + } +} diff --git a/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/MonitorCDS.java b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/MonitorCDS.java new file mode 100644 index 0000000..8a05e37 --- /dev/null +++ b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/MonitorCDS.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 1996-2001 Sun Microsystems, Inc. All Rights Reserved. + * + * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, + * modify and redistribute this software in source and binary code form, + * provided that i) this copyright notice and license appear on all copies of + * the software; and ii) Licensee does not utilize the software in a manner + * which is disparaging to Sun. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY + * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR + * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE + * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING + * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS + * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, + * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER + * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF + * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * This software is not designed or intended for use in on-line control of + * aircraft, air traffic, aircraft navigation or aircraft communications; or in + * the design, construction, operation or maintenance of any nuclear + * facility. Licensee represents and warrants that it will not use or + * redistribute the Software for such purposes. + */ +package jp.nyatla.nyartoolkit.jmf.utils; + + +import javax.media.*; +import javax.media.protocol.*; +import javax.media.control.*; + +import java.io.IOException; + + + +public class MonitorCDS extends PushBufferDataSource{ + + private PushBufferDataSource delegate = null; + private PushBufferStream [] delStreams = null; + private MonitorStream monitorStream = null; + private PushBufferStream [] monitorStreams = null; + boolean delStarted = false; // variable used by MonitorStream also + private Control [] controls; + + public MonitorCDS(DataSource ds) + { + // Get the stream from the actual datasource + // and create a MonitorStream from it + // Export the MonitorControl interface of the MonitorStream + if (ds instanceof PushBufferDataSource) + { + delegate = (PushBufferDataSource) ds; + delStreams = delegate.getStreams(); + monitorStream = new MonitorStream(delStreams[0], this); + monitorStreams = new PushBufferStream[] {monitorStream}; + } + } + + public Object [] getControls() + { + return controls; + } + + public Object getControl(String value) { + if (value.equals("jmfsample.MonitorStream") || value.equals("javax.media.control.MonitorControl")) + return monitorStream; + else + return null; + } + + public javax.media.CaptureDeviceInfo getCaptureDeviceInfo() + { + return ((CaptureDevice)delegate).getCaptureDeviceInfo(); + } + + public FormatControl[] getFormatControls() + { + return ((CaptureDevice)delegate).getFormatControls(); + } + + public String getContentType() + { + return delegate.getContentType(); + } + + public void connect() throws IOException + { + if (delegate == null) + throw new IOException("Incompatible DataSource"); + // Delegate is already connected + } + + public void disconnect() + { + monitorStream.setEnabled(false); + delegate.disconnect(); + } + + public synchronized void start() throws IOException + { + startDelegate(); + delStarted = true; + } + + public synchronized void stop() throws IOException + { + if (!monitorStream.isEnabled()) { + stopDelegate(); + } + delStarted = false; + } + + public Time getDuration() + { + return delegate.getDuration(); + } + + public PushBufferStream [] getStreams() + { + return monitorStreams; + } + + void startDelegate() throws IOException + { + delegate.start(); + } + + void stopDelegate() throws IOException + { + delegate.stop(); + } + +} diff --git a/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/MonitorStream.java b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/MonitorStream.java new file mode 100644 index 0000000..28d6b86 --- /dev/null +++ b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/MonitorStream.java @@ -0,0 +1,196 @@ +/* + * Copyright (c) 1996-2001 Sun Microsystems, Inc. All Rights Reserved. + * + * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, + * modify and redistribute this software in source and binary code form, + * provided that i) this copyright notice and license appear on all copies of + * the software; and ii) Licensee does not utilize the software in a manner + * which is disparaging to Sun. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY + * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR + * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE + * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING + * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS + * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, + * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER + * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF + * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * This software is not designed or intended for use in on-line control of + * aircraft, air traffic, aircraft navigation or aircraft communications; or in + * the design, construction, operation or maintenance of any nuclear + * facility. Licensee represents and warrants that it will not use or + * redistribute the Software for such purposes. + */ +package jp.nyatla.nyartoolkit.jmf.utils; + + + +import javax.media.*; +import javax.media.protocol.*; + +import javax.media.util.BufferToImage; +import java.io.IOException; +import java.awt.*; + +public class MonitorStream implements PushBufferStream, BufferTransferHandler { + + JmfCaptureListener img_listener; + PushBufferStream actual = null; + boolean dataAvailable = false; + boolean terminate = false; + boolean enabled = false; + Object bufferLock = new Object(); + Buffer cbuffer = new Buffer(); + BufferTransferHandler transferHandler = null; + Component component = null; + MonitorCDS cds; + BufferToImage bti = null; + + MonitorStream(PushBufferStream actual, MonitorCDS cds) { + this.actual = actual; + actual.setTransferHandler(this); + this.cds = cds; + } + + public javax.media.Format getFormat() + { + return actual.getFormat(); + } + /** + * 非同期READ + */ + public void read(Buffer buffer) throws IOException + { + // Wait for data to be available + // Doesn't get used much because the transferData + // call is made when data IS available. And most + // Processors/Players read the data in the same + // thread that called transferData, although that's + // not a safe assumption to make + if (!dataAvailable) { + synchronized (bufferLock) { + while (!dataAvailable && !terminate) { + try { + bufferLock.wait(100); + } catch (InterruptedException ie) { + } + } + } + } + + if (dataAvailable) { + synchronized (bufferLock) { + // Copy the buffer attributes, but swap the data + // attributes so that no extra copy is made. + buffer.copy(cbuffer, true); + //dataAvailable = false; + } + } +// return; + } + public void setCaptureListener(JmfCaptureListener i_listener) + { + img_listener=i_listener; + } + + public void transferData(PushBufferStream pbs) + { + // Get the data from the original source stream + synchronized (bufferLock) { + try { + pbs.read(cbuffer); + } catch (IOException ioe) { + return; + } + dataAvailable = true; + bufferLock.notifyAll(); + } + if(img_listener!=null){ + img_listener.onUpdateBuffer(cbuffer); + } + +/* + // Display data if monitor is active + if (isEnabled()) { + if (bti == null) { + VideoFormat vf = (VideoFormat) cbuffer.getFormat(); + bti = new BufferToImage(vf); + } + if (bti != null && component != null) { + Image im = bti.createImage(cbuffer); + Graphics g = component.getGraphics(); + Dimension size = component.getSize(); + if (g != null) + g.drawImage(im, 0, 0, component); + } + } +*/ + // Maybe synchronize this with setTransferHandler() ? + if (transferHandler != null && cds.delStarted) + transferHandler.transferData(this); + } + + public void setTransferHandler(BufferTransferHandler transferHandler) { + this.transferHandler = transferHandler; + } + + public boolean setEnabled(boolean value) { + enabled = value; + if (value == false) { + if (!cds.delStarted) { + try { + cds.stopDelegate(); + } catch (IOException ioe) { + } + } + } else { + // Start the capture datasource if the monitor is enabled + try { + cds.startDelegate(); + }catch (IOException ioe) { + } + } + return enabled; + } + + public boolean isEnabled() + { + return enabled; + } + + + + public float setPreviewFrameRate(float rate) + { + System.err.println("TODO"); + return rate; + } + + public ContentDescriptor getContentDescriptor() + { + return actual.getContentDescriptor(); + } + + public long getContentLength() + { + return actual.getContentLength(); + } + + public boolean endOfStream() { + return actual.endOfStream(); + } + + public Object [] getControls() { + return new Object[0]; + } + + public Object getControl(String str) { + return null; + } + + +} diff --git a/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/sample/JmfCaptureTest.java b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/sample/JmfCaptureTest.java new file mode 100644 index 0000000..aa8e435 --- /dev/null +++ b/tags/2.0.0/src.utils/jmf/jp/nyatla/nyartoolkit/jmf/utils/sample/JmfCaptureTest.java @@ -0,0 +1,82 @@ +/* + * PROJECT: NyARToolkit JMF utilities. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package jp.nyatla.nyartoolkit.jmf.utils.sample; + +import javax.media.*; + +import javax.media.util.BufferToImage; +import javax.media.format.*; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.jmf.utils.*; + +import java.awt.*; + +/** + * VFMキャプチャテストプログラム + */ +public class JmfCaptureTest extends Frame implements JmfCaptureListener +{ + public JmfCaptureTest() throws NyARException + { + setTitle("JmfCaptureTest"); + setBounds(0, 0, 320 + 64, 240 + 64); + capture = new JmfCameraCapture(320, 240, 30f, JmfCameraCapture.PIXEL_FORMAT_RGB); + capture.setCaptureListener(this); + } + + private JmfCameraCapture capture; + + public void onUpdateBuffer(Buffer i_buffer) + { + BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat()); + Image img = b2i.createImage(i_buffer); + Graphics g = getGraphics(); + g.drawImage(img, 32, 32, this); + } + + private void startCapture() + { + try { + capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) + { + try { + JmfCaptureTest mainwin = new JmfCaptureTest(); + mainwin.setVisible(true); + mainwin.startCapture(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/tags/2.0.0/src.utils/jogl/.classpath b/tags/2.0.0/src.utils/jogl/.classpath new file mode 100644 index 0000000..d2b8f57 --- /dev/null +++ b/tags/2.0.0/src.utils/jogl/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tags/2.0.0/src.utils/jogl/.project b/tags/2.0.0/src.utils/jogl/.project new file mode 100644 index 0000000..43c3381 --- /dev/null +++ b/tags/2.0.0/src.utils/jogl/.project @@ -0,0 +1,17 @@ + + + NyARToolkit.utils.jogl + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARDetectMarker.java b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARDetectMarker.java new file mode 100644 index 0000000..9bf44e6 --- /dev/null +++ b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARDetectMarker.java @@ -0,0 +1,95 @@ +/* + * PROJECT: NyARToolkit JOGL utilities. + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.jogl.utils; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARCode; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; +import jp.nyatla.nyartoolkit.detector.NyARDetectMarker; + +/** + * NyARSingleDetectMarkerにOpenGL向け関数を追加したもの + */ +public class GLNyARDetectMarker extends NyARDetectMarker +{ + private NyARTransMatResult trans_mat_result = new NyARTransMatResult(); + + private double view_scale_factor = 0.025; + + public GLNyARDetectMarker(NyARParam i_param, NyARCode[] i_code, double[] i_marker_width, int i_number_of_code) throws NyARException + { + super(i_param, i_code, i_marker_width, i_number_of_code); + } + + public void setScaleFactor(double i_new_value) + { + view_scale_factor = i_new_value; + } + + /** + * @param i_index + * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 + * @param o_result + * 結果値を格納する配列を指定してください。double[16]以上が必要です。 + * @throws NyARException + */ + public void getCameraViewRH(int i_index, double[] o_result) throws NyARException + { + // 座標を計算 + this.getTransmationMatrix(i_index, this.trans_mat_result); + // 行列変換 + final NyARTransMatResult mat = this.trans_mat_result; + o_result[0 + 0 * 4] = mat.m00; + o_result[0 + 1 * 4] = mat.m01; + o_result[0 + 2 * 4] = mat.m02; + o_result[0 + 3 * 4] = mat.m03; + o_result[1 + 0 * 4] = -mat.m10; + o_result[1 + 1 * 4] = -mat.m11; + o_result[1 + 2 * 4] = -mat.m12; + o_result[1 + 3 * 4] = -mat.m13; + o_result[2 + 0 * 4] = -mat.m20; + o_result[2 + 1 * 4] = -mat.m21; + o_result[2 + 2 * 4] = -mat.m22; + o_result[2 + 3 * 4] = -mat.m23; + o_result[3 + 0 * 4] = 0.0; + o_result[3 + 1 * 4] = 0.0; + o_result[3 + 2 * 4] = 0.0; + o_result[3 + 3 * 4] = 1.0; + if (view_scale_factor != 0.0) { + o_result[12] *= view_scale_factor; + o_result[13] *= view_scale_factor; + o_result[14] *= view_scale_factor; + } + return; + } +} diff --git a/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARParam.java b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARParam.java new file mode 100644 index 0000000..416d1be --- /dev/null +++ b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARParam.java @@ -0,0 +1,127 @@ +/* + * PROJECT: NyARToolkit JOGL utilities. + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.jogl.utils; + +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; + +/** + * NyARParamにOpenGL向け関数を追加したもの + */ +public class GLNyARParam extends NyARParam +{ + private double view_distance_min = 0.1;//#define VIEW_DISTANCE_MIN 0.1 // Objects closer to the camera than this will not be displayed. + + private double view_distance_max = 100.0;//#define VIEW_DISTANCE_MAX 100.0 // Objects further away from the camera than this will not be displayed. + + private double[] m_projection = null; + + public void setViewDistanceMin(double i_new_value) + { + m_projection = null;//キャッシュ済変数初期化 + view_distance_min = i_new_value; + } + + public void setViewDistanceMax(double i_new_value) + { + m_projection = null;//キャッシュ済変数初期化 + view_distance_max = i_new_value; + } + + /** + * void arglCameraFrustumRH(const ARParam *cparam, const double focalmin, const double focalmax, GLdouble m_projection[16]) + * 関数の置き換え + * @param focalmin + * @param focalmax + * @return + */ + public double[] getCameraFrustumRH() + { + //既に値がキャッシュされていたらそれを使う + if (m_projection != null) { + return m_projection; + } + //無ければ計算 + m_projection = new double[16]; + NyARMat trans_mat = new NyARMat(3, 4); + NyARMat icpara_mat = new NyARMat(3, 4); + double[][] p = new double[3][3], q = new double[4][4]; + + int i, j; + + final int width = this._screen_size.w; + final int height = this._screen_size.h; + + this.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat); + + double[][] icpara = icpara_mat.getArray(); + double[][] trans = trans_mat.getArray(); + for (i = 0; i < 4; i++) { + icpara[1][i] = (height - 1) * (icpara[2][i]) - icpara[1][i]; + } + + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + p[i][j] = icpara[i][j] / icpara[2][2]; + } + } + q[0][0] = (2.0 * p[0][0] / (width - 1)); + q[0][1] = (2.0 * p[0][1] / (width - 1)); + q[0][2] = -((2.0 * p[0][2] / (width - 1)) - 1.0); + q[0][3] = 0.0; + + q[1][0] = 0.0; + q[1][1] = -(2.0 * p[1][1] / (height - 1)); + q[1][2] = -((2.0 * p[1][2] / (height - 1)) - 1.0); + q[1][3] = 0.0; + + q[2][0] = 0.0; + q[2][1] = 0.0; + q[2][2] = (view_distance_max + view_distance_min) / (view_distance_min - view_distance_max); + q[2][3] = 2.0 * view_distance_max * view_distance_min / (view_distance_min - view_distance_max); + + q[3][0] = 0.0; + q[3][1] = 0.0; + q[3][2] = -1.0; + q[3][3] = 0.0; + + for (i = 0; i < 4; i++) { // Row. + // First 3 columns of the current row. + for (j = 0; j < 3; j++) { // Column. + m_projection[i + j * 4] = q[i][0] * trans[0][j] + q[i][1] * trans[1][j] + q[i][2] * trans[2][j]; + } + // Fourth column of the current row. + m_projection[i + 3 * 4] = q[i][0] * trans[0][3] + q[i][1] * trans[1][3] + q[i][2] * trans[2][3] + q[i][3]; + } + return m_projection; + } +} diff --git a/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARRaster_RGB.java b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARRaster_RGB.java new file mode 100644 index 0000000..1d28f27 --- /dev/null +++ b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARRaster_RGB.java @@ -0,0 +1,112 @@ +/* + * PROJECT: NyARToolkit JOGL utilities. + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.jogl.utils; + +import javax.media.format.RGBFormat; +import javax.media.opengl.GL; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.jmf.utils.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.rasterreader.*; + + +/** + * NyARRaster_RGBにOpenGL用のデータ変換機能を追加したものです。 + */ +public class GLNyARRaster_RGB extends JmfNyARRaster_RGB +{ + private byte[] _gl_buf; + + private int _gl_flag; + + public GLNyARRaster_RGB(NyARParam i_param) + { + super(i_param.getScreenSize()); + this._gl_flag = GL.GL_RGB; + this._gl_buf = new byte[this._size.w * this._size.h * 3]; + } + + public void setBuffer(javax.media.Buffer i_buffer, boolean i_is_reverse) throws NyARException + { + int buffer_type=analyzeBufferType((RGBFormat) i_buffer.getFormat());; + byte[] src_buf = (byte[]) i_buffer.getData(); + // GL用のデータを準備 + if (i_is_reverse) { + final int length = this._size.w * 3; + int src_idx = 0; + int dest_idx = (this._size.h - 1) * length; + for (int i = 0; i < this._size.h; i++) { + System.arraycopy(src_buf, src_idx, this._gl_buf, dest_idx, length); + src_idx += length; + dest_idx -= length; + } + } else { + System.arraycopy(src_buf, 0, this._gl_buf, 0, src_buf.length); + } + + // GLのフラグ設定 + switch (buffer_type) { + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24: + this._gl_flag = GL.GL_BGR; + break; + case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24: + this._gl_flag = GL.GL_RGB; + break; + default: + throw new NyARException(); + } + // ref_bufをgl_bufに差し替える + this._ref_buf = this._gl_buf; + this._reader.changeBuffer(buffer_type, this._ref_buf); + } + + /** + * GLでそのまま描画できるRGBバッファを返す。 + * + * @return + */ + public byte[] getGLRgbArray() + { + return this._ref_buf; + } + + /** + * GL用のRGBバッファのバイト並びタイプを返す。 + * + * @return + */ + public int getGLPixelFlag() + { + return this._gl_flag; + } +} diff --git a/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARSingleDetectMarker.java b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARSingleDetectMarker.java new file mode 100644 index 0000000..c1d79c6 --- /dev/null +++ b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARSingleDetectMarker.java @@ -0,0 +1,103 @@ +/* + * PROJECT: NyARToolkit JOGL utilities. + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.jogl.utils; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARCode; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; +import jp.nyatla.nyartoolkit.detector.NyARSingleDetectMarker; + + +/** + * NyARSingleDetectMarkerにOpenGL向け関数を追加したもの + */ +public class GLNyARSingleDetectMarker extends NyARSingleDetectMarker +{ + private NyARTransMatResult trans_mat_result = new NyARTransMatResult(); + + private double view_scale_factor = 0.025;// #define VIEW_SCALEFACTOR 0.025 // 1.0 ARToolKit unit becomes 0.025 of my OpenGL units. + + public GLNyARSingleDetectMarker(NyARParam i_param, NyARCode i_code, double i_marker_width) throws NyARException + { + super(i_param, i_code, i_marker_width); + } + + public void setScaleFactor(double i_new_value) + { + view_scale_factor = i_new_value; + } + + // public static void arglCameraViewRH(const double para[3][4], GLdouble m_modelview[16], const double scale) + public double[] getCameraViewRH() throws NyARException + { + double[] result = new double[16]; + getCameraViewRH(result); + return result; + } + + /** + * + * @param o_result + * 結果値を格納する配列を指定してください。double[16]以上が必要です。 + * @throws NyARException + */ + public void getCameraViewRH(double[] o_result) throws NyARException + { + // 座標を計算 + this.getTransmationMatrix(this.trans_mat_result); + // 行列変換 + final NyARTransMatResult mat = this.trans_mat_result; + o_result[0 + 0 * 4] = mat.m00; // R1C1 + o_result[0 + 1 * 4] = mat.m01; // R1C2 + o_result[0 + 2 * 4] = mat.m02; + o_result[0 + 3 * 4] = mat.m03; + o_result[1 + 0 * 4] = -mat.m10; // R2 + o_result[1 + 1 * 4] = -mat.m11; + o_result[1 + 2 * 4] = -mat.m12; + o_result[1 + 3 * 4] = -mat.m13; + o_result[2 + 0 * 4] = -mat.m20; // R3 + o_result[2 + 1 * 4] = -mat.m21; + o_result[2 + 2 * 4] = -mat.m22; + o_result[2 + 3 * 4] = -mat.m23; + o_result[3 + 0 * 4] = 0.0; + o_result[3 + 1 * 4] = 0.0; + o_result[3 + 2 * 4] = 0.0; + o_result[3 + 3 * 4] = 1.0; + if (view_scale_factor != 0.0) { + o_result[12] *= view_scale_factor; + o_result[13] *= view_scale_factor; + o_result[14] *= view_scale_factor; + } + return; + } +} diff --git a/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/NyARGLUtil.java b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/NyARGLUtil.java new file mode 100644 index 0000000..f26c2f9 --- /dev/null +++ b/tags/2.0.0/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/NyARGLUtil.java @@ -0,0 +1,128 @@ +/* + * PROJECT: NyARToolkit JOGL utilities. + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.jogl.utils; + +import java.nio.ByteBuffer; +import java.nio.IntBuffer; + +import javax.media.opengl.GL; +import javax.media.opengl.glu.GLU; + +import jp.nyatla.nyartoolkit.core.types.*; +/** + * NyARToolkit用のJOGL支援関数群 + */ +public class NyARGLUtil +{ + private javax.media.opengl.GL _gl; + + private javax.media.opengl.glu.GLU _glu; + + public NyARGLUtil(javax.media.opengl.GL i_gl) + { + this._gl = i_gl; + this._glu = new GLU(); + } + + /** + * GLNyARRaster_RGBをバックグラウンドに書き出す。 + * @param image + * @param zoom + */ + public void drawBackGround(GLNyARRaster_RGB i_raster, double i_zoom) + { + IntBuffer texEnvModeSave = IntBuffer.allocate(1); + boolean lightingSave; + boolean depthTestSave; + javax.media.opengl.GL gl = this._gl; + final NyARIntSize rsize=i_raster.getSize(); + + // Prepare an orthographic projection, set camera position for 2D drawing, and save GL state. + gl.glGetTexEnviv(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, texEnvModeSave); // Save GL texture environment mode. + if (texEnvModeSave.array()[0] != GL.GL_REPLACE) { + gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE); + } + lightingSave = gl.glIsEnabled(GL.GL_LIGHTING); // Save enabled state of lighting. + if (lightingSave == true) { + gl.glDisable(GL.GL_LIGHTING); + } + depthTestSave = gl.glIsEnabled(GL.GL_DEPTH_TEST); // Save enabled state of depth test. + if (depthTestSave == true) { + gl.glDisable(GL.GL_DEPTH_TEST); + } + gl.glMatrixMode(GL.GL_PROJECTION); + gl.glPushMatrix(); + gl.glLoadIdentity(); + _glu.gluOrtho2D(0.0,rsize.w, 0.0,rsize.h); + gl.glMatrixMode(GL.GL_MODELVIEW); + gl.glPushMatrix(); + gl.glLoadIdentity(); + arglDispImageStateful(i_raster, i_zoom); + + // Restore previous projection, camera position, and GL state. + gl.glMatrixMode(GL.GL_PROJECTION); + gl.glPopMatrix(); + gl.glMatrixMode(GL.GL_MODELVIEW); + gl.glPopMatrix(); + if (depthTestSave) { + gl.glEnable(GL.GL_DEPTH_TEST); // Restore enabled state of depth test. + } + if (lightingSave) { + gl.glEnable(GL.GL_LIGHTING); // Restore enabled state of lighting. + } + if (texEnvModeSave.get(0) != GL.GL_REPLACE) { + gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, texEnvModeSave.get(0)); // Restore GL texture environment mode. + } + gl.glEnd(); + } + + /** + * arglDispImageStateful関数モドキ + * @param image + * @param zoom + */ + private void arglDispImageStateful(GLNyARRaster_RGB i_raster, double zoom) + { + javax.media.opengl.GL gl_ = this._gl; + final NyARIntSize rsize = i_raster.getSize(); + float zoomf; + IntBuffer params = IntBuffer.allocate(4); + zoomf = (float) zoom; + gl_.glDisable(GL.GL_TEXTURE_2D); + gl_.glGetIntegerv(GL.GL_VIEWPORT, params); + gl_.glPixelZoom(zoomf * ((float) (params.get(2)) / (float) rsize.w), -zoomf * ((float) (params.get(3)) / (float) rsize.h)); + gl_.glWindowPos2f(0.0f, (float) rsize.h); + gl_.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); + ByteBuffer buf = ByteBuffer.wrap(i_raster.getGLRgbArray()); + gl_.glDrawPixels(rsize.w,rsize.h, i_raster.getGLPixelFlag(), GL.GL_UNSIGNED_BYTE, buf); + } +} diff --git a/tags/2.0.0/src.utils/qt/.classpath b/tags/2.0.0/src.utils/qt/.classpath new file mode 100644 index 0000000..76c4ee4 --- /dev/null +++ b/tags/2.0.0/src.utils/qt/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/tags/2.0.0/src.utils/qt/.project b/tags/2.0.0/src.utils/qt/.project new file mode 100644 index 0000000..149fb9e --- /dev/null +++ b/tags/2.0.0/src.utils/qt/.project @@ -0,0 +1,17 @@ + + + NyARToolkit.utils.qt + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtCameraCapture.java b/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtCameraCapture.java new file mode 100644 index 0000000..24f54fa --- /dev/null +++ b/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtCameraCapture.java @@ -0,0 +1,238 @@ +/* + * PROJECT: NyARToolkit Quicktime utilities. + * -------------------------------------------------------------------------------- + * Copyright (C)2008 arc@dmz + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * + * + * + */ + +package jp.nyatla.nyartoolkit.qt.utils; + +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; + +import javax.swing.Timer; + +import quicktime.QTException; +import quicktime.QTSession; +import quicktime.io.QTFile; +import quicktime.qd.PixMap; +import quicktime.qd.QDConstants; +import quicktime.qd.QDGraphics; +import quicktime.qd.QDRect; +import quicktime.std.StdQTConstants; +import quicktime.std.movies.Movie; +import quicktime.std.movies.media.DataRef; +import quicktime.std.sg.SGVideoChannel; +import quicktime.std.sg.SequenceGrabber; +import quicktime.util.RawEncodedImage; + +import jp.nyatla.nyartoolkit.NyARException; + +/** + * QuickTimeキャプチャクラス + * + */ +public class QtCameraCapture implements ActionListener +{ + + private Dimension image_size; + + private QtCaptureListener capture_listener; + + protected float fps = 30; // キャプチャ画像を取得するfps + + protected byte[] pixels; // キャプチャ画像の実データを保持するバイト型配列 + + // キャプチャに使うタイマー + private Timer timer; + + // QTJava用のあれこれ + private QDGraphics graphics; + + private QDRect bounds; + + private SequenceGrabber grabber; + + private SGVideoChannel channel; + + private RawEncodedImage rawEncodedImage; + + private Movie movie; + + // ピクセルフォーマット変換用の一時変数 + private int[] pixels_int; + + public static final int PIXEL_FORMAT_RGB = quicktime.util.EndianOrder.isNativeLittleEndian() ? QDConstants.k32BGRAPixelFormat : QDGraphics.kDefaultPixelFormat; + + public QtCameraCapture(int i_width, int i_height, float i_rate) + { + image_size = new Dimension(i_width, i_height); + fps = i_rate; + } + + public Dimension getSize() + { + return image_size; + } + + public byte[] readBuffer() throws NyARException + { + if (grabber == null) { + throw new NyARException(); + } + return pixels; + } + + public void setCaptureListener(QtCaptureListener i_listener) throws NyARException + { + if (grabber != null) { + throw new NyARException(); + } + capture_listener = i_listener; + + } + + public void prepSetInput(Object input) throws QTException + { + QTSession.open(); + bounds = new QDRect(image_size.width, image_size.height); + graphics = new QDGraphics(quicktime.util.EndianOrder.isNativeLittleEndian() ? QDConstants.k32BGRAPixelFormat : QDGraphics.kDefaultPixelFormat, bounds); + if (input != null && input.getClass().equals(File.class)) { + movie = quicktime.std.movies.Movie.fromDataRef(new DataRef(new QTFile((File) input)), StdQTConstants.newMovieActive); + } else { + grabber = new SequenceGrabber(); + grabber.setGWorld(graphics, null); + channel = new SGVideoChannel(grabber); + channel.setBounds(bounds); + + // seqGrabPreview == 2, Processingでmagic numberとしてハードコートされていた… + channel.setUsage(StdQTConstants.seqGrabPreview); + + if (input != null) { + try { + channel.setDevice(input.toString()); + } catch (QTException e) { + e.printStackTrace(); + throw e; + } + } + } + } + + /** + * Webcamの設定ダイアログを表示する。 + * 既定のWebcamでは駄目な場合(複数のWebcamが接続されているPCなど)ではこれを実行するとよい。 + */ + public void prepShowDialog() throws QTException + { + channel.settingsDialog(); + } + + public void start() throws NyARException + { + try { + + if (grabber == null) + prepSetInput(null); + + if (movie == null) { + grabber.prepare(true, false); // あってもなくてもよさそう + grabber.startPreview(); + } else { + movie.preroll(0, 1.0f); + while (movie.maxLoadedTimeInMovie() == 0) + movie.task(100); + movie.setRate(1); + movie.getPict(movie.getTime()).draw(graphics, bounds); + } + PixMap pixmap = graphics.getPixMap(); + rawEncodedImage = pixmap.getPixelData(); + + image_size.width = rawEncodedImage.getRowBytes() / 4; + pixels = new byte[image_size.width * image_size.height * 3]; + pixels_int = new int[image_size.width * image_size.height]; + } catch (QTException e) { + QTSession.close(); + throw new NyARException(e); + } + + // キャプチャイメージを定期的に更新するタイマー + timer = new Timer((int) (1000 / fps), this); + timer.start(); + } + + public void stop() + { + finalize(); + } + + /** タイマー処理。キャプチャイメージの更新結果をリスナに伝える。 */ + public void actionPerformed(ActionEvent event) + { + + // 画像をQTJavaのRawEncodedImageとして取得 + try { + if (movie == null) { + grabber.idle(); + } else { + if (movie.isDone()) + movie.goToBeginning(); + movie.getPict(movie.getTime()).draw(graphics, bounds); + } + } catch (QTException e) { + QTSession.close(); + e.printStackTrace(); + } + + // RawEncodedImageをint列に落とし込む + rawEncodedImage.copyToArray(0, pixels_int, 0, pixels_int.length); + + // バイト列を生成する + int idx_byte = 0; + for (int idx = 0; idx < image_size.width * image_size.height; idx++) { + pixels[idx_byte++] = (byte) (pixels_int[idx] >> 16); + pixels[idx_byte++] = (byte) (pixels_int[idx] >> 8 & 0xff); + pixels[idx_byte++] = (byte) (pixels_int[idx] & 0xff); + } + + // 各リスナに更新されたバイト列を渡す + capture_listener.onUpdateBuffer(pixels); + } + + protected void finalize() + { + try { + if (movie == null) { + grabber.stop(); + grabber.release(); + grabber.disposeChannel(channel); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + QTSession.close(); + } + timer.stop(); + } + +} diff --git a/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtCaptureListener.java b/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtCaptureListener.java new file mode 100644 index 0000000..eaef79c --- /dev/null +++ b/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtCaptureListener.java @@ -0,0 +1,35 @@ +/* + * PROJECT: NyARToolkit Quicktime utilities. + * -------------------------------------------------------------------------------- + * Copyright (C)2008 arc@dmz + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * + * + * + */ +package jp.nyatla.nyartoolkit.qt.utils; + +/** + * QuickTime リスナクラス + * + */ +public interface QtCaptureListener +{ + public void onUpdateBuffer(byte[] i_buffer); + +} \ No newline at end of file diff --git a/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtNyARRaster_RGB.java b/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtNyARRaster_RGB.java new file mode 100644 index 0000000..3c96abd --- /dev/null +++ b/tags/2.0.0/src.utils/qt/jp/nyatla/nyartoolkit/qt/utils/QtNyARRaster_RGB.java @@ -0,0 +1,140 @@ +/* + * PROJECT: NyARToolkit Quicktime utilities. + * -------------------------------------------------------------------------------- + * Copyright (C)2008 arc@dmz + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * + * + * + */ +package jp.nyatla.nyartoolkit.qt.utils; + +import java.awt.image.BufferedImage; +import java.awt.image.DataBuffer; +import java.awt.image.WritableRaster; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.rgb.NyARRgbRaster_BasicClass; +import jp.nyatla.nyartoolkit.core.rasterreader.*; +import jp.nyatla.nyartoolkit.core.types.*; +/** + * RGB形式のbyte配列をラップするNyARRasterです。 + * 保持したデータからBufferedImageを出力する機能も持ちます。 + */ +public class QtNyARRaster_RGB extends NyARRgbRaster_BasicClass +{ + private class PixcelReader extends NyARRgbPixelReader_RGB24 implements INyARBufferReader + { + public PixcelReader(NyARIntSize i_size) + { + super(null, i_size); + return; + } + + public void syncBuffer(byte[] i_ref_buffer) + { + this._ref_buf = i_ref_buffer; + return; + } + + // + // INyARBufferReader + // + public Object getBuffer() + { + return this._ref_buf; + } + + public int getBufferType() + { + return INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24; + } + + public boolean isEqualBufferType(int i_type_value) + { + return i_type_value == INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24; + } + } + + protected byte[] _ref_buf; + + protected PixcelReader _reader; + + private WritableRaster _raster; + + private BufferedImage _image; + + /** + * RGB形式のJMFバッファをラップするオブジェクトをつくります。 生成直後のオブジェクトはデータを持ちません。 メンバ関数はsetBufferを実行後に使用可能になります。 + */ + public QtNyARRaster_RGB(int i_width, int i_height) + { + super(new NyARIntSize(i_width,i_height)); + this._ref_buf = null; + this._reader = new PixcelReader(this._size); + _raster = WritableRaster.createInterleavedRaster(DataBuffer.TYPE_BYTE, i_width, i_height, i_width * 3, 3, new int[] { 0, 1, 2 }, null); + _image = new BufferedImage(i_width, i_height, BufferedImage.TYPE_3BYTE_BGR); + } + + /** + * javax.media.Bufferを分析して、その分析結果をNyARRasterに適合する形で保持します。 関数実行後に外部でi_bufferの内容変更した場合には、再度setBuffer関数を呼び出してください。 + * + * @param i_buffer + * RGB形式のデータを格納したjavax.media.Bufferオブジェクトを指定してください。 + * @return i_bufferをラップしたオブジェクトを返します。 + * @throws NyARException + */ + public void setBuffer(byte[] i_buffer) + { + this._ref_buf = i_buffer; + this._reader.syncBuffer(i_buffer); + } + + public INyARBufferReader getBufferReader() + { + return this._reader; + } + + public INyARRgbPixelReader getRgbPixelReader() + { + return this._reader; + } + + /** + * データを持っているかを返します。 + * + * @return + */ + public boolean hasData() + { + return this._ref_buf != null; + } + + /** + * 保持しているデータからBufferedImageを作って返します。 + * + * @return + */ + public BufferedImage createImage() + { + _raster.setDataElements(0, 0, this._size.w, this._size.h, this._ref_buf); + _image.setData(_raster); + return _image; + } + +} diff --git a/tags/2.0.0/src/.classpath b/tags/2.0.0/src/.classpath new file mode 100644 index 0000000..6d488d7 --- /dev/null +++ b/tags/2.0.0/src/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/tags/2.0.0/src/.project b/tags/2.0.0/src/.project new file mode 100644 index 0000000..84e4f3f --- /dev/null +++ b/tags/2.0.0/src/.project @@ -0,0 +1,17 @@ + + + NyARToolKit + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/NyARException.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/NyARException.java new file mode 100644 index 0000000..058e56a --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/NyARException.java @@ -0,0 +1,62 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit; + +public class NyARException extends Exception +{ + private static final long serialVersionUID = 1L; + + public NyARException() + { + super(); + } + + public NyARException(Exception e) + { + super(e); + } + + public NyARException(String m) + { + super(m); + } + + public static void trap(String m) throws NyARException + { + throw new NyARException("トラップ:" + m); + } + + public static void notImplement() throws NyARException + { + throw new NyARException("Not Implement!"); + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/INyARSquareDetector.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/INyARSquareDetector.java new file mode 100644 index 0000000..08d73b2 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/INyARSquareDetector.java @@ -0,0 +1,40 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster; + +public interface INyARSquareDetector +{ + public void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARCode.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARCode.java new file mode 100644 index 0000000..886b716 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARCode.java @@ -0,0 +1,198 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StreamTokenizer; + +import jp.nyatla.nyartoolkit.NyARException; + + + +/** + * ARToolKitのマーカーコードを1個保持します。 + * + */ +public class NyARCode +{ + private int[][][][] pat;// static int + // pat[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3]; + + private double[] patpow = new double[4];// static double patpow[AR_PATT_NUM_MAX][4]; + + private short[][][] patBW;// static int patBW[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3]; + + private double[] patpowBW = new double[4];// static double patpowBW[AR_PATT_NUM_MAX][4]; + + private int width, height; + + public int[][][][] getPat() + { + return pat; + } + + public double[] getPatPow() + { + return patpow; + } + + public short[][][] getPatBW() + { + return patBW; + } + + public double[] getPatPowBW() + { + return patpowBW; + } + + public int getWidth() + { + return width; + } + + public int getHeight() + { + return height; + } + + public NyARCode(int i_width, int i_height) + { + width = i_width; + height = i_height; + pat = new int[4][height][width][3];// static int pat[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3]; + patBW = new short[4][height][width];// static int patBW[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3]; + } + + /** + * int arLoadPatt( const char *filename ); ARToolKitのパターンファイルをロードする。 + * ファイル形式はBGR形式で記録されたパターンファイルであること。 + * + * @param filename + * @return + * @throws Exception + */ + public void loadARPattFromFile(String filename) throws NyARException + { + try { + loadARPatt(new FileInputStream(filename)); + + } catch (Exception e) { + throw new NyARException(e); + } + } + + /** + * + * @param i_stream + * @throws NyARException + */ + public void loadARPatt(InputStream i_stream) throws NyARException + { + try { + StreamTokenizer st = new StreamTokenizer(new InputStreamReader( + i_stream)); + // パターンデータはGBRAで並んでる。 + for (int h = 0; h < 4; h++) { + int l = 0; + for (int i3 = 0; i3 < 3; i3++) { + for (int i2 = 0; i2 < height; i2++) { + for (int i1 = 0; i1 < width; i1++) { + // 数値のみ読み出す + switch (st.nextToken()) {// if( fscanf(fp, "%d",&j) != 1 ) { + case StreamTokenizer.TT_NUMBER: + break; + default: + throw new NyARException(); + } + short j = (short) (255 - st.nval);// j = 255-j; + // 標準ファイルのパターンはBGRでならんでるからRGBに並べなおす + switch (i3) { + case 0: + pat[h][i2][i1][2] = j; + break;// pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+2]= j;break; + case 1: + pat[h][i2][i1][1] = j; + break;// pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+1]= j;break; + case 2: + pat[h][i2][i1][0] = j; + break;// pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+0]= j;break; + } + // pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+i3]= j; + if (i3 == 0) { + patBW[h][i2][i1] = j;// patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1] = j; + } else { + patBW[h][i2][i1] += j;// patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1] += j; + } + if (i3 == 2) { + patBW[h][i2][i1] /= 3;// patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1]/= 3; + } + l += j; + } + } + } + + l /= (height * width * 3); + + int m = 0; + for (int i = 0; i < height; i++) {// for( i = 0; i < AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3;i++ ) { + for (int i2 = 0; i2 < width; i2++) { + for (int i3 = 0; i3 < 3; i3++) { + pat[h][i][i2][i3] -= l; + m += (pat[h][i][i2][i3] * pat[h][i][i2][i3]); + } + } + } + patpow[h] = Math.sqrt((double) m); + if (patpow[h] == 0.0) { + patpow[h] = 0.0000001; + } + + m = 0; + for (int i = 0; i < height; i++) { + for (int i2 = 0; i2 < width; i2++) { + patBW[h][i][i2] -= l; + m += (patBW[h][i][i2] * patBW[h][i][i2]); + } + } + patpowBW[h] = Math.sqrt((double) m); + if (patpowBW[h] == 0.0) { + patpowBW[h] = 0.0000001; + } + } + } catch (Exception e) { + throw new NyARException(e); + } + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARMat.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARMat.java new file mode 100644 index 0000000..28314f5 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARMat.java @@ -0,0 +1,1184 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core; + +import jp.nyatla.nyartoolkit.NyARException; + + + +/** + * ARMat構造体に対応するクラス typedef struct { double *m; int row; int clm; }ARMat; + * + */ +public class NyARMat +{ + /** + * 配列サイズと行列サイズは必ずしも一致しないことに注意 返された配列のサイズを行列の大きさとして使わないこと! + * + */ + protected double[][] m; + + private int clm, row; + + /** + * デフォルトコンストラクタは機能しません。 + * + * @throws NyARException + */ + protected NyARMat() throws NyARException + { + throw new NyARException(); + } + + public NyARMat(int i_row, int i_clm) + { + m = new double[i_row][i_clm]; + clm = i_clm; + row = i_row; + } + + /** + * i_row x i_clmサイズの行列を格納できるように行列サイズを変更します。 実行後、行列の各値は不定になります。 + * + * @param i_row + * @param i_clm + */ + public void realloc(int i_row, int i_clm) + { + if (i_row <= this.m.length && i_clm <= this.m[0].length) { + // 十分な配列があれば何もしない。 + } else { + // 不十分なら取り直す。 + m = new double[i_row][i_clm]; + } + this.clm = i_clm; + this.row = i_row; + } + + public int getClm() + { + return clm; + } + + public int getRow() + { + return row; + } + + /** + * 行列をゼロクリアする。 + */ + public void zeroClear() + { + int i, i2; + // For順変更OK + for (i = row - 1; i >= 0; i--) { + for (i2 = clm - 1; i2 >= 0; i2--) { + m[i][i2] = 0.0; + } + } + } + + /** + * i_copy_fromの内容を自分自身にコピーします。 高さ・幅は同一で無いと失敗します。 + * + * @param i_copy_from + */ + public void copyFrom(NyARMat i_copy_from) throws NyARException + { + // サイズ確認 + if (this.row != i_copy_from.row || this.clm != i_copy_from.clm) { + throw new NyARException(); + } + // 値コピー + for (int r = this.row - 1; r >= 0; r--) { + for (int c = this.clm - 1; c >= 0; c--) { + this.m[r][c] = i_copy_from.m[r][c]; + } + } + } + + public double[][] getArray() + { + return m; + } + + // public void getRowVec(int i_row,NyARVec o_vec) + // { + // o_vec.set(this.m[i_row],this.clm); + // } + /** + * aとbの積を自分自身に格納する。arMatrixMul()の代替品 + * + * @param a + * @param b + * @throws NyARException + */ + public void matrixMul(NyARMat a, NyARMat b) throws NyARException + { + if (a.clm != b.row || this.row != a.row || this.clm != b.clm) { + throw new NyARException(); + } + double w; + int r, c, i; + double[][] am = a.m, bm = b.m, dm = this.m; + // For順変更禁止 + for (r = 0; r < this.row; r++) { + for (c = 0; c < this.clm; c++) { + w = 0.0;// dest.setARELEM0(r, c,0.0); + for (i = 0; i < a.clm; i++) { + w += am[r][i] * bm[i][c];// ARELEM0(dest, r, c) +=ARELEM0(a, r, i) * ARELEM0(b,i, c); + } + dm[r][c] = w; + } + } + } + + private int[] wk_nos_matrixSelfInv = new int[50]; + + // private final static double matrixSelfInv_epsl=1.0e-10; + /** + * i_targetを逆行列に変換する。arMatrixSelfInv()と、arMatrixSelfInv_minv()関数を合成してあります。 + * OPTIMIZE STEP[485->422] + * + * @param i_target + * 逆行列にする行列 + * @return 逆行列があればTRUE/無ければFALSE + * + * @throws NyARException + */ + public boolean matrixSelfInv() throws NyARException + { + double[][] ap = this.m; + int dimen = this.row; + int dimen_1 = dimen - 1; + double[] ap_n, ap_ip, ap_i;// wap; + int j, ip, nwork; + int[] nos = wk_nos_matrixSelfInv;// この関数で初期化される。 + // double epsl; + double p, pbuf, work; + + /* check size */ + switch (dimen) { + case 0: + throw new NyARException(); + case 1: + ap[0][0] = 1.0 / ap[0][0];// *ap = 1.0 / (*ap); + return true;/* 1 dimension */ + } + + for (int n = 0; n < dimen; n++) { + nos[n] = n; + } + + /* + * nyatla memo ipが定まらないで計算が行われる場合があるので挿入。 ループ内で0初期化していいかが判らない。 + */ + ip = 0; + // For順変更禁止 + for (int n = 0; n < dimen; n++) { + ap_n = ap[n];// wcp = ap + n * rowa; + p = 0.0; + for (int i = n; i < dimen; i++) {// for(i = n, wap = wcp, p = + // 0.0; i < dimen ; i++, wap += + // rowa) + if (p < (pbuf = Math.abs(ap[i][0]))) { + p = pbuf; + ip = i; + } + } + // if (p <= matrixSelfInv_epsl){ + if (p == 0.0) { + return false; + // throw new NyARException(); + } + + nwork = nos[ip]; + nos[ip] = nos[n]; + nos[n] = nwork; + + ap_ip = ap[ip]; + for (j = 0; j < dimen; j++) {// for(j = 0, wap = ap + ip * rowa, + // wbp = wcp; j < dimen ; j++) { + work = ap_ip[j]; // work = *wap; + ap_ip[j] = ap_n[j]; + ap_n[j] = work; + } + + work = ap_n[0]; + for (j = 0; j < dimen_1; j++) {// for(j = 1, wap = wcp, work = + // *wcp; j < dimen ; j++, wap++) + ap_n[j] = ap_n[j + 1] / work;// *wap = *(wap + 1) / work; + } + ap_n[j] = 1.0 / work;// *wap = 1.0 / work; + for (int i = 0; i < dimen; i++) { + if (i != n) { + ap_i = ap[i];// wap = ap + i * rowa; + + work = ap_i[0]; + for (j = 0; j < dimen_1; j++) {// for(j = 1, wbp = wcp,work = *wap;j < dimen ;j++, wap++, wbp++) + ap_i[j] = ap_i[j + 1] - work * ap_n[j];// wap = *(wap +1) - work *(*wbp); + } + ap_i[j] = -work * ap_n[j];// *wap = -work * (*wbp); + } + } + } + + for (int n = 0; n < dimen; n++) { + for (j = n; j < dimen; j++) { + if (nos[j] == n) { + break; + } + } + nos[j] = nos[n]; + for (int i = 0; i < dimen; i++) {// for(i = 0, wap = ap + j, wbp + // = ap + n; i < dimen ;i++, wap + // += rowa, wbp += rowa) { + ap_i = ap[i]; + work = ap_i[j];// work = *wap; + ap_i[j] = ap_i[n];// *wap = *wbp; + ap_i[n] = work;// *wbp = work; + } + } + return true; + } + + /** + * sourceの転置行列をdestに得る。arMatrixTrans()の代替品 + * + * @param dest + * @param source + * @return + */ + public static void matrixTrans(NyARMat dest, NyARMat source) throws NyARException + { + if (dest.row != source.clm || dest.clm != source.row) { + throw new NyARException(); + } + NyARException.trap("未チェックのパス"); + // For順変更禁止 + for (int r = 0; r < dest.row; r++) { + for (int c = 0; c < dest.clm; c++) { + dest.m[r][c] = source.m[c][r]; + } + } + } + + /** + * unitを単位行列に初期化する。arMatrixUnitの代替品 + * + * @param unit + */ + public static void matrixUnit(NyARMat unit) throws NyARException + { + if (unit.row != unit.clm) { + throw new NyARException(); + } + NyARException.trap("未チェックのパス"); + // For順変更禁止 + for (int r = 0; r < unit.getRow(); r++) { + for (int c = 0; c < unit.getClm(); c++) { + if (r == c) { + unit.m[r][c] = 1.0; + } else { + unit.m[r][c] = 0.0; + } + } + } + } + + /** + * sourceの内容を自身に複製する。 Optimized 2008.04.19 + * + * @param i_source + * @return + */ + public void matrixDup(NyARMat i_source) throws NyARException + { + // 自身の配列サイズを相手のそれより大きいことを保障する。 + this.realloc(i_source.row, i_source.clm); + // 内容を転写 + int r, c; + double[][] src_m, dest_m; + src_m = i_source.m; + dest_m = this.m; + // コピーはFor順を変えてもOK + for (r = this.row - 1; r >= 0; r--) { + for (c = this.clm - 1; c >= 0; c--) { + dest_m[r][c] = src_m[r][c]; + } + } + } + + public NyARMat matrixAllocDup() throws NyARException + { + NyARMat result = new NyARMat(this.row, this.clm); + // コピー + int r, c; + double[][] dest_m, src_m; + dest_m = result.m; + src_m = this.m; + // コピーはFor順を変えてもOK + for (r = this.row - 1; r >= 0; r--) { + for (c = this.clm - 1; c >= 0; c--) { + dest_m[r][c] = src_m[r][c]; + } + } + return result; + } + + /** + * arMatrixInv関数の代替品です。 destにsourceの逆行列を返します。 + * + * @param dest + * @param source + * @throws NyARException + */ + public static void matrixInv(NyARMat dest, NyARMat source) + throws NyARException + { + NyARException.trap("未チェックのパス"); + dest.matrixDup(source); + + NyARException.trap("未チェックのパス"); + dest.matrixSelfInv(); + } + + public NyARMat matrixAllocInv() throws NyARException + { + NyARException.trap("未チェックのパス"); + NyARMat result = matrixAllocDup(); + + NyARException.trap("未チェックのパス"); + result.matrixSelfInv(); + return result; + } + + /** + * dim x dim の単位行列を作る。 + * + * @param dim + * @return + * @throws NyARException + */ + public static NyARMat matrixAllocUnit(int dim) throws NyARException + { + NyARException.trap("未チェックのパス"); + NyARMat result = new NyARMat(dim, dim); + NyARException.trap("未チェックのパス"); + NyARMat.matrixUnit(result); + return result; + } + + /** + * arMatrixDispの代替品 + * + * @param m + * @return + */ + public int matrixDisp() throws NyARException + { + NyARException.trap("未チェックのパス"); + System.out.println(" === matrix (" + row + "," + clm + ") ===");// printf(" ===matrix (%d,%d) ===\n", m->row, m->clm); + for (int r = 0; r < row; r++) {// for(int r = 0; r < m->row; r++) { + System.out.print(" |");// printf(" |"); + for (int c = 0; c < clm; c++) {// for(int c = 0; c < m->clm; c++) { + System.out.print(" " + m[r][c]);// printf(" %10g", ARELEM0(m, r, c)); + } + System.out.println(" |");// printf(" |\n"); + } + System.out.println(" ======================");// printf(" ======================\n"); + return 0; + } + + private static final double PCA_EPS = 1e-6; // #define EPS 1e-6 + + private static final int PCA_MAX_ITER = 100; // #define MAX_ITER 100 + + private static final double PCA_VZERO = 1e-16; // #define VZERO 1e-16 + + /** + * static int EX( ARMat *input, ARVec *mean )の代替関数 Optimize:STEP:[144->110] + * + * @param input + * @param mean + * @return + * @throws NyARException + */ + private void PCA_EX(NyARVec mean) throws NyARException + { + int lrow, lclm; + int i, i2; + lrow = this.row; + lclm = this.clm; + double[][] lm = this.m; + + if (lrow <= 0 || lclm <= 0) { + throw new NyARException(); + } + if (mean.getClm() != lclm) { + throw new NyARException(); + } + // double[] mean_array=mean.getArray(); + // mean.zeroClear(); + final double[] mean_array = mean.getArray(); + double w; + // For順変更禁止 + for (i2 = 0; i2 < lclm; i2++) { + w = 0.0; + for (i = 0; i < lrow; i++) { + // *(v++) += *(m++); + w += lm[i][i2]; + } + mean_array[i2] = w / lrow;// mean->v[i] /= row; + } + } + + /** + * static int CENTER( ARMat *inout, ARVec *mean )の代替関数 + * + * @param inout + * @param mean + * @return + */ + private static void PCA_CENTER(NyARMat inout, NyARVec mean)throws NyARException + { + double[] v; + int row, clm; + + row = inout.getRow(); + clm = inout.getClm(); + if (mean.getClm() != clm) { + throw new NyARException(); + } + double[][] im = inout.m; + double[] im_i; + double w0, w1; + v = mean.getArray(); + // 特にパフォーマンスが劣化するclm=1と2ときだけ、別パスで処理します。 + switch (clm) { + case 1: + w0 = v[0]; + for (int i = 0; i < row; i++) { + im[i][0] -= w0; + } + break; + case 2: + w0 = v[0]; + w1 = v[1]; + for (int i = 0; i < row; i++) { + im_i = im[i]; + im_i[0] -= w0; + im_i[1] -= w1; + } + break; + default: + for (int i = 0; i < row; i++) { + im_i = im[i]; + for (int j = 0; j < clm; j++) { + // *(m++) -= *(v++); + im_i[j] -= v[j]; + } + } + break; + } + return; + } + + /** + * int x_by_xt( ARMat *input, ARMat *output )の代替関数 + * + * @param input + * @param output + * @throws NyARException + */ + private static void PCA_x_by_xt(NyARMat input, NyARMat output) throws NyARException + { + NyARException.trap("動作未チェック/配列化未チェック"); + int row, clm; + // double[][] out; + double[] in1, in2; + + NyARException.trap("未チェックのパス"); + row = input.row; + clm = input.clm; + NyARException.trap("未チェックのパス"); + if (output.row != row || output.clm != row) { + throw new NyARException(); + } + + // out = output.getArray(); + for (int i = 0; i < row; i++) { + for (int j = 0; j < row; j++) { + if (j < i) { + NyARException.trap("未チェックのパス"); + output.m[i][j] = output.m[j][i];// *out = + // output->m[j*row+i]; + } else { + NyARException.trap("未チェックのパス"); + in1 = input.m[i];// input.getRowArray(i);//in1 = &(input->m[clm*i]); + in2 = input.m[j];// input.getRowArray(j);//in2 = &(input->m[clm*j]); + output.m[i][j] = 0;// *out = 0.0; + for (int k = 0; k < clm; k++) { + output.m[i][j] += (in1[k] * in2[k]);// *out += *(in1++) + // * *(in2++); + } + } + // out.incPtr(); + } + } + } + + /** + * static int xt_by_x( ARMat *input, ARMat *output )の代替関数 + * Optimize:2008.04.19 + * + * @param input + * @param i_output + * @throws NyARException + */ + private static void PCA_xt_by_x(NyARMat input, NyARMat i_output) throws NyARException + { + double[] in_; + int row, clm; + + row = input.row; + clm = input.clm; + if (i_output.row != clm || i_output.clm != clm) { + throw new NyARException(); + } + + int k, j; + double[][] out_m = i_output.m; + double w; + for (int i = 0; i < clm; i++) { + for (j = 0; j < clm; j++) { + if (j < i) { + out_m[i][j] = out_m[j][i];// *out = output->m[j*clm+i]; + } else { + w = 0.0;// *out = 0.0; + for (k = 0; k < row; k++) { + in_ = input.m[k];// in=input.getRowArray(k); + w += (in_[i] * in_[j]);// *out += *in1 * *in2; + } + out_m[i][j] = w; + } + } + } + } + + private final NyARVec wk_PCA_QRM_ev = new NyARVec(1); + + /** + * static int QRM( ARMat *a, ARVec *dv )の代替関数 + * + * @param a + * @param dv + * @throws NyARException + */ + private void PCA_QRM(NyARVec dv) throws NyARException + { + double w, t, s, x, y, c; + int dim, iter; + double[] dv_array = dv.getArray(); + + dim = this.row; + if (dim != this.clm || dim < 2) { + throw new NyARException(); + } + if (dv.getClm() != dim) { + throw new NyARException(); + } + + NyARVec ev = this.wk_PCA_QRM_ev; + ev.realloc(dim); + double[] ev_array = ev.getArray(); + if (ev == null) { + throw new NyARException(); + } + final double[][] L_m = this.m; + this.vecTridiagonalize(dv, ev, 1); + + ev_array[0] = 0.0;// ev->v[0] = 0.0; + for (int h = dim - 1; h > 0; h--) { + int j = h; + while (j > 0&& Math.abs(ev_array[j]) > PCA_EPS* (Math.abs(dv_array[j - 1]) + Math.abs(dv_array[j]))) {// while(j>0 && fabs(ev->v[j]) >EPS*(fabs(dv->v[j-1])+fabs(dv->v[j]))) + // j--; + j--; + } + if (j == h) { + continue; + } + iter = 0; + do { + iter++; + if (iter > PCA_MAX_ITER) { + break; + } + w = (dv_array[h - 1] - dv_array[h]) / 2;// w = (dv->v[h-1] -dv->v[h]) / 2;//ここ? + t = ev_array[h] * ev_array[h];// t = ev->v[h] * ev->v[h]; + s = Math.sqrt(w * w + t); + if (w < 0) { + s = -s; + } + x = dv_array[j] - dv_array[h] + t / (w + s);// x = dv->v[j] -dv->v[h] +t/(w+s); + y = ev_array[j + 1];// y = ev->v[j+1]; + for (int k = j; k < h; k++) { + if (Math.abs(x) >= Math.abs(y)) { + if (Math.abs(x) > PCA_VZERO) { + t = -y / x; + c = 1 / Math.sqrt(t * t + 1); + s = t * c; + } else { + c = 1.0; + s = 0.0; + } + } else { + t = -x / y; + s = 1.0 / Math.sqrt(t * t + 1); + c = t * s; + } + w = dv_array[k] - dv_array[k + 1];// w = dv->v[k] -dv->v[k+1]; + t = (w * s + 2 * c * ev_array[k + 1]) * s;// t = (w * s +2 * c *ev->v[k+1]) *s; + dv_array[k] -= t;// dv->v[k] -= t; + dv_array[k + 1] += t;// dv->v[k+1] += t; + if (k > j) { + NyARException.trap("未チェックパス"); + { + ev_array[k] = c * ev_array[k] - s * y;// ev->v[k]= c *ev->v[k]- s * y; + } + } + ev_array[k + 1] += s * (c * w - 2 * s * ev_array[k + 1]);// ev->v[k+1]+= s * (c* w- 2* s *ev->v[k+1]); + + for (int i = 0; i < dim; i++) { + x = L_m[k][i];// x = a->m[k*dim+i]; + y = L_m[k + 1][i];// y = a->m[(k+1)*dim+i]; + L_m[k][i] = c * x - s * y;// a->m[k*dim+i] = c * x - s* y; + L_m[k + 1][i] = s * x + c * y;// a->m[(k+1)*dim+i] = s* x + c * y; + } + if (k < h - 1) { + NyARException.trap("未チェックパス"); + { + x = ev_array[k + 1];// x = ev->v[k+1]; + y = -s * ev_array[k + 2];// y = -s * ev->v[k+2]; + ev_array[k + 2] *= c;// ev->v[k+2] *= c; + } + } + } + } while (Math.abs(ev_array[h]) > PCA_EPS + * (Math.abs(dv_array[h - 1]) + Math.abs(dv_array[h]))); + } + for (int k = 0; k < dim - 1; k++) { + int h = k; + t = dv_array[h];// t = dv->v[h]; + for (int i = k + 1; i < dim; i++) { + if (dv_array[i] > t) {// if( dv->v[i] > t ) { + h = i; + t = dv_array[h];// t = dv->v[h]; + } + } + dv_array[h] = dv_array[k];// dv->v[h] = dv->v[k]; + dv_array[k] = t;// dv->v[k] = t; + this.flipRow(h, k); + } + } + + /** + * i_row_1番目の行と、i_row_2番目の行を入れ替える。 + * + * @param i_row_1 + * @param i_row_2 + */ + private void flipRow(int i_row_1, int i_row_2) + { + int i; + double w; + double[] r1 = this.m[i_row_1], r2 = this.m[i_row_2]; + // For順変更OK + for (i = clm - 1; i >= 0; i--) { + w = r1[i]; + r1[i] = r2[i]; + r2[i] = w; + } + } + + /** + * static int EV_create( ARMat *input, ARMat *u, ARMat *output, ARVec *ev + * )の代替関数 + * + * @param input + * @param u + * @param output + * @param ev + * @throws NyARException + */ + private static void PCA_EV_create(NyARMat input, NyARMat u, NyARMat output,NyARVec ev) throws NyARException + { + NyARException.trap("未チェックのパス"); + int row, clm; + row = input.row;// row = input->row; + clm = input.clm;// clm = input->clm; + if (row <= 0 || clm <= 0) { + throw new NyARException(); + } + if (u.row != row || u.clm != row) {// if( u->row != row || u->clm != + // row ){ + throw new NyARException(); + } + if (output.row != row || output.clm != clm) {// if( output->row != + // row || output->clm != + // clm ){ + throw new NyARException(); + } + if (ev.getClm() != row) {// if( ev->clm != row ){ + throw new NyARException(); + } + double[][] m, in_; + double[] m1, ev_array; + double sum, work; + + NyARException.trap("未チェックのパス"); + m = output.m;// m = output->m; + in_ = input.m; + int i; + ev_array = ev.getArray(); + for (i = 0; i < row; i++) { + NyARException.trap("未チェックのパス"); + if (ev_array[i] < PCA_VZERO) {// if( ev->v[i] < VZERO ){ + break; + } + NyARException.trap("未チェックのパス"); + work = 1 / Math.sqrt(Math.abs(ev_array[i]));// work = 1 / + // sqrt(fabs(ev->v[i])); + for (int j = 0; j < clm; j++) { + sum = 0.0; + m1 = u.m[i];// m1 = &(u->m[i*row]); + // m2=input.getPointer(j);//m2 = &(input->m[j]); + for (int k = 0; k < row; k++) { + sum += m1[k] + in_[k][j];// sum += *m1 * *m2; + // m1.incPtr(); //m1++; + // m2.addPtr(clm);//m2 += clm; + } + m1[j] = sum * work;// *(m++) = sum * work; + // {//*(m++) = sum * work; + // m.set(sum * work); + // m.incPtr();} + } + } + for (; i < row; i++) { + NyARException.trap("未チェックのパス"); + ev_array[i] = 0.0;// ev->v[i] = 0.0; + for (int j = 0; j < clm; j++) { + m[i][j] = 0.0; + // m.set(0.0);//*(m++) = 0.0; + // m.incPtr(); + } + } + } + + private NyARMat wk_PCA_PCA_u = null; + + /** + * static int PCA( ARMat *input, ARMat *output, ARVec *ev ) + * + * @param output + * @param o_ev + * @throws NyARException + */ + private void PCA_PCA(NyARMat o_output, NyARVec o_ev) throws NyARException + { + + int l_row, l_clm, min; + double[] ev_array = o_ev.getArray(); + + l_row = this.row;// row = input->row; + l_clm = this.clm;// clm = input->clm; + min = (l_clm < l_row) ? l_clm : l_row; + if (l_row < 2 || l_clm < 2) { + throw new NyARException(); + } + if (o_output.clm != this.clm) {// if( output->clm != input->clm ){ + throw new NyARException(); + } + if (o_output.row != min) {// if( output->row != min ){ + throw new NyARException(); + } + if (o_ev.getClm() != min) {// if( ev->clm != min ){ + throw new NyARException(); + } + + NyARMat u;// u =new NyARMat( min, min ); + if (this.wk_PCA_PCA_u == null) { + u = new NyARMat(min, min); + this.wk_PCA_PCA_u = u; + } else { + u = this.wk_PCA_PCA_u; + u.realloc(min, min); + } + + if (l_row < l_clm) { + NyARException.trap("未チェックのパス"); + PCA_x_by_xt(this, u);// if(x_by_xt( input, u ) < 0 ) { + } else { + PCA_xt_by_x(this, u);// if(xt_by_x( input, u ) < 0 ) { + } + u.PCA_QRM(o_ev); + + double[][] m1, m2; + if (l_row < l_clm) { + NyARException.trap("未チェックのパス"); + PCA_EV_create(this, u, o_output, o_ev); + } else { + m1 = u.m;// m1 = u->m; + m2 = o_output.m;// m2 = output->m; + int i; + for (i = 0; i < min; i++) { + if (ev_array[i] < PCA_VZERO) {// if( ev->v[i] < VZERO ){ + break; + } + for (int j = 0; j < min; j++) { + m2[i][j] = m1[i][j];// *(m2++) = *(m1++); + } + } + for (; i < min; i++) {// for( ; i < min; i++){ + // コードを見た限りあってそうだからコメントアウト(2008/03/26)NyARException.trap("未チェックのパス"); + ev_array[i] = 0.0;// ev->v[i] = 0.0; + for (int j = 0; j < min; j++) { + m2[i][j] = 0.0;// *(m2++) = 0.0; + } + } + } + } + + private NyARMat wk_work_matrixPCA = null; + + /** + * int arMatrixPCA( ARMat *input, ARMat *evec, ARVec *ev, ARVec *mean ); + * 関数の置き換え。input引数がthisになる。 Optimize:2008.04.19 + * + * @param o_evec + * @param o_ev + * + * @param mean + * @throws NyARException + */ + public void matrixPCA(NyARMat o_evec, NyARVec o_ev, NyARVec mean)throws NyARException + { + double srow, sum; + int l_row, l_clm; + int check; + + l_row = this.row;// row = input->row; + l_clm = this.clm;// clm = input->clm; + check = (l_row < l_clm) ? l_row : l_clm; + if (l_row < 2 || l_clm < 2) { + throw new NyARException(); + } + if (o_evec.clm != l_clm || o_evec.row != check) {// if( evec->clm != + // input->clm || + // evec->row != + // check ){ + throw new NyARException(); + } + if (o_ev.getClm() != check) {// if( ev->clm != check ){ + throw new NyARException(); + } + if (mean.getClm() != l_clm) {// if( mean->clm != input->clm ){ + throw new NyARException(); + } + + // 自分の内容をワークにコピー(高速化の為に、1度作ったインスタンスは使いまわす) + NyARMat work; + if (this.wk_work_matrixPCA == null) { + work = this.matrixAllocDup(); + this.wk_work_matrixPCA = work; + } else { + work = this.wk_work_matrixPCA; + work.matrixDup(this);// arMatrixAllocDup( input );work = + // arMatrixAllocDup( input ); + } + + srow = Math.sqrt((double) l_row); + work.PCA_EX(mean); + + PCA_CENTER(work, mean); + + int i, j; + // For順変更OK + for (i = 0; i < l_row; i++) { + for (j = 0; j < l_clm; j++) { + work.m[i][j] /= srow;// work->m[i] /= srow; + } + } + + work.PCA_PCA(o_evec, o_ev); + + sum = 0.0; + double[] ev_array = o_ev.getArray(); + int ev_clm = o_ev.getClm(); + // For順変更禁止 + for (i = 0; i < ev_clm; i++) {// for(int i = 0; i < ev->clm; i++ ){ + sum += ev_array[i];// sum += ev->v[i]; + } + // For順変更禁止 + for (i = 0; i < ev_clm; i++) {// for(int i = 0; i < ev->clm; i++ ){ + ev_array[i] /= sum;// ev->v[i] /= sum; + } + } + + /* int arMatrixPCA2( ARMat *input, ARMat *evec, ARVec *ev ); */ + public static void arMatrixPCA2(NyARMat input, NyARMat evec, NyARVec ev) throws NyARException + { + NyARException.trap("未チェックのパス"); + NyARMat work; + // double srow; // unreferenced + double sum; + int row, clm; + int check; + + row = input.row;// row = input->row; + clm = input.clm;// clm = input->clm; + check = (row < clm) ? row : clm; + if (row < 2 || clm < 2) { + throw new NyARException(); + } + if (evec.getClm() != input.clm || evec.row != check) {// if( evec->clm!= input->clm|| evec->row!= check ){ + throw new NyARException(); + } + if (ev.getClm() != check) {// if( ev->clm != check ){ + throw new NyARException(); + } + + NyARException.trap("未チェックのパス"); + work = input.matrixAllocDup(); + + NyARException.trap("未チェックパス"); + work.PCA_PCA(evec, ev);// rval = PCA( work, evec, ev ); + sum = 0.0; + double[] ev_array = ev.getArray(); + for (int i = 0; i < ev.getClm(); i++) {// for( i = 0; i < ev->clm; i++ + // ){ + NyARException.trap("未チェックパス"); + sum += ev_array[i];// sum += ev->v[i]; + } + for (int i = 0; i < ev.getClm(); i++) {// for(int i = 0; i < ev->clm;i++ ){ + NyARException.trap("未チェックパス"); + ev_array[i] /= sum;// ev->v[i] /= sum; + } + return; + } + + public static NyARMat matrixAllocMul(NyARMat a, NyARMat b) throws NyARException + { + NyARException.trap("未チェックのパス"); + NyARMat dest = new NyARMat(a.row, b.clm); + NyARException.trap("未チェックのパス"); + dest.matrixMul(a, b); + return dest; + } + + /* static double mdet(double *ap, int dimen, int rowa) */ + private static double Det_mdet(double[][] ap, int dimen, int rowa) throws NyARException + { + NyARException.trap("動作未チェック/配列化未チェック"); + double det = 1.0; + double work; + int is_ = 0; + int mmax; + + for (int k = 0; k < dimen - 1; k++) { + mmax = k; + for (int i = k + 1; i < dimen; i++) { + // if (Math.abs(arMatrixDet_MATRIX_get(ap, i, k, rowa)) > + // Math.abs(arMatrixDet_MATRIX_get(ap, mmax, k, rowa))){ + if (Math.abs(ap[i][k]) > Math.abs(ap[mmax][k])) { + mmax = i; + } + } + if (mmax != k) { + for (int j = k; j < dimen; j++) { + work = ap[k][j];// work = MATRIX(ap, k, j, rowa); + ap[k][j] = ap[mmax][j];// MATRIX(ap, k, j, rowa) =MATRIX(ap, mmax, j, rowa); + ap[mmax][j] = work;// MATRIX(ap, mmax, j, rowa) = work; + } + is_++; + } + for (int i = k + 1; i < dimen; i++) { + work = ap[i][k] / ap[k][k];// work = arMatrixDet_MATRIX_get(ap,i, k, rowa) /arMatrixDet_MATRIX_get(ap, k, k,rowa); + for (int j = k + 1; j < dimen; j++) { + // MATRIX(ap, i, j, rowa) -= work * MATRIX(ap, k, j, rowa); + ap[i][j] -= work * ap[k][j]; + } + } + } + for (int i = 0; i < dimen; i++) { + det = ap[i][i];// det *= MATRIX(ap, i, i, rowa); + } + for (int i = 0; i < is_; i++) { + det *= -1.0; + } + return det; + } + + /* double arMatrixDet(ARMat *m); */ + public static double arMatrixDet(NyARMat m) throws NyARException + { + NyARException.trap("動作未チェック/配列化未チェック"); + if (m.row != m.clm) { + return 0.0; + } + return Det_mdet(m.getArray(), m.row, m.clm);// return mdet(m->m, m->row,m->row); + } + + private final NyARVec wk_vecTridiagonalize_vec = new NyARVec(0); + + private final NyARVec wk_vecTridiagonalize_vec2 = new NyARVec(0); + + /** + * arVecTridiagonalize関数の代替品 a,d,e間で演算をしてる。何をどうしているかはさっぱりさっぱり + * + * @param a + * @param d + * @param e + * @param i_e_start + * 演算開始列(よくわからないけどarVecTridiagonalizeの呼び出し元でなんかしてる) + * @return + * @throws NyARException + */ + private void vecTridiagonalize(NyARVec d, NyARVec e, int i_e_start)throws NyARException + { + NyARVec vec = wk_vecTridiagonalize_vec; + // double[][] a_array=a.getArray(); + double s, t, p, q; + int dim; + + if (this.clm != this.row) {// if(a.getClm()!=a.getRow()){ + throw new NyARException(); + } + if (this.clm != d.getClm()) {// if(a.getClm() != d.clm){ + throw new NyARException(); + } + if (this.clm != e.getClm()) {// if(a.getClm() != e.clm){ + throw new NyARException(); + } + dim = this.getClm(); + + double[] d_vec, e_vec; + d_vec = d.getArray(); + e_vec = e.getArray(); + double[] a_vec_k; + + for (int k = 0; k < dim - 2; k++) { + + a_vec_k = this.m[k]; + vec.setNewArray(a_vec_k, clm);// vec=this.getRowVec(k);//double[] + // vec_array=vec.getArray(); + NyARException.trap("未チェックパス"); + d_vec[k] = a_vec_k[k];// d.v[k]=vec.v[k];//d.set(k,v.get(k)); + // //d->v[k] = v[k]; + + // wv1.clm = dim-k-1; + // wv1.v = &(v[k+1]); + NyARException.trap("未チェックパス"); + e_vec[k + i_e_start] = vec.vecHousehold(k + 1);// e.v[k+i_e_start]=vec.vecHousehold(k+1);//e->v[k]= arVecHousehold(&wv1); + if (e_vec[k + i_e_start] == 0.0) {// if(e.v[k+i_e_start]== 0.0){//if(e.v[k+i_e_start]== 0.0){ + continue; + } + + for (int i = k + 1; i < dim; i++) { + s = 0.0; + for (int j = k + 1; j < i; j++) { + NyARException.trap("未チェックのパス"); + s += this.m[j][i] * a_vec_k[j];// s += a_array[j][i] *vec.v[j];//s +=a.get(j*dim+i) *v.get(j);//s +=a->m[j*dim+i] * v[j]; + } + for (int j = i; j < dim; j++) { + NyARException.trap("未チェックのパス"); + s += this.m[i][j] * a_vec_k[j];// s += a_array[i][j] *vec.v[j];//s +=a.get(i*dim+j) *v.get(j);//s +=a->m[i*dim+j] * v[j]; + } + NyARException.trap("未チェックのパス"); + d_vec[i] = s;// d.v[i]=s;//d->v[i] = s; + } + + // wv1.clm = wv2.clm = dim-k-1; + // wv1.v = &(v[k+1]); + // wv2.v = &(d->v[k+1]); + a_vec_k = this.m[k]; + vec.setNewArray(a_vec_k, clm);// vec=this.getRowVec(k); + // vec_array=vec.getArray(); + NyARException.trap("未チェックパス"); + t = vec.vecInnerproduct(d, k + 1) / 2; + for (int i = dim - 1; i > k; i--) { + NyARException.trap("未チェックパス"); + p = a_vec_k[i];// p = v.get(i);//p = v[i]; + d_vec[i] -= t * p; + q = d_vec[i];// d.v[i]-=t*p;q=d.v[i];//q = d->v[i] -= t*p; + for (int j = i; j < dim; j++) { + NyARException.trap("未チェックパス"); + this.m[i][j] -= p * (d_vec[j] + q * a_vec_k[j]);// a.m[i][j]-=p*(d.v[j] +q*vec.v[j]);//a->m[i*dim+j] -=p*(d->v[j]) + q*v[j]; + } + } + } + + if (dim >= 2) { + d_vec[dim - 2] = this.m[dim - 2][dim - 2];// d.v[dim-2]=a.m[dim-2][dim-2];//d->v[dim-2]=a->m[(dim-2)*dim+(dim-2)]; + e_vec[dim - 2 + i_e_start] = this.m[dim - 2][dim - 1];// e.v[dim-2+i_e_start]=a.m[dim-2][dim-1];//e->v[dim-2] = a->m[(dim-2)*dim+(dim-1)]; + } + + if (dim >= 1) { + d_vec[dim - 1] = this.m[dim - 1][dim - 1];// d.v[dim-1]=a_array[dim-1][dim-1];//d->v[dim-1] =a->m[(dim-1)*dim+(dim-1)]; + } + NyARVec vec2 = this.wk_vecTridiagonalize_vec2; + for (int k = dim - 1; k >= 0; k--) { + a_vec_k = this.m[k]; + vec.setNewArray(a_vec_k, clm);// vec=this.getRowVec(k);//v =a.getPointer(k*dim);//v = &(a->m[k*dim]); + if (k < dim - 2) { + for (int i = k + 1; i < dim; i++) { + // wv1.clm = wv2.clm = dim-k-1; + // wv1.v = &(v[k+1]); + // wv2.v = &(a->m[i*dim+k+1]); + vec2.setNewArray(this.m[i], clm);// vec2=this.getRowVec(i); + + t = vec.vecInnerproduct(vec2, k + 1); + for (int j = k + 1; j < dim; j++) { + NyARException.trap("未チェックパス"); + this.m[i][j] -= t * a_vec_k[j];// a_array[i][j]-=t*vec.v[j];//a.subValue(i*dim+j,t*v.get(j));//a->m[i*dim+j]-= t * v[j]; + } + } + } + for (int i = 0; i < dim; i++) { + a_vec_k[i] = 0.0;// v.set(i,0.0);//v[i] = 0.0; + } + a_vec_k[k] = 1;// v.set(k,1);//v[k] = 1; + } + return; + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquare.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquare.java new file mode 100644 index 0000000..78f18e9 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquare.java @@ -0,0 +1,52 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core; + +import jp.nyatla.nyartoolkit.core.types.*; +/** + * ARMarkerInfoに相当するクラス。 矩形情報を保持します。 + * + */ +public class NyARSquare +{ + public NyARLinear[] line = new NyARLinear[4]; + public NyARDoublePoint2d[] sqvertex = new NyARDoublePoint2d[4]; + public NyARIntPoint[] imvertex = new NyARIntPoint[4]; + public NyARSquare() + { + for(int i=0;i<4;i++){ + this.sqvertex[i]=new NyARDoublePoint2d(); + this.imvertex[i]=new NyARIntPoint(); + this.line[i]=new NyARLinear(); + } + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquareDetector.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquareDetector.java new file mode 100644 index 0000000..5ffcb5f --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquareDetector.java @@ -0,0 +1,525 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core; +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.labeling.*; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.core.param.*; + + + + + +/** + * イメージから正方形候補を検出するクラス。 + * このクラスは、arDetectMarker2.cとの置き換えになります。 + * + */ +public class NyARSquareDetector implements INyARSquareDetector +{ + private static final double VERTEX_FACTOR = 1.0;// 線検出のファクタ + + private static final int AR_AREA_MAX = 100000;// #define AR_AREA_MAX 100000 + + private static final int AR_AREA_MIN = 70;// #define AR_AREA_MIN 70 + private final int _width; + private final int _height; + + private final NyARLabeling_ARToolKit _labeling; + + private final NyARLabelingImage _limage; + + private final OverlapChecker _overlap_checker = new OverlapChecker(); + private final NyARCameraDistortionFactor _dist_factor_ref; + + /** + * 最大i_squre_max個のマーカーを検出するクラスを作成する。 + * + * @param i_param + */ + public NyARSquareDetector(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException + { + this._width = i_size.w; + this._height = i_size.h; + this._dist_factor_ref = i_dist_factor_ref; + this._labeling = new NyARLabeling_ARToolKit(); + this._limage = new NyARLabelingImage(this._width, this._height); + this._labeling.attachDestination(this._limage); + + // 輪郭の最大長は画面に映りうる最大の長方形サイズ。 + int number_of_coord = (this._width + this._height) * 2; + + // 輪郭バッファは頂点変換をするので、輪郭バッファの2倍取る。 + this._max_coord = number_of_coord; + this._xcoord = new int[number_of_coord * 2]; + this._ycoord = new int[number_of_coord * 2]; + } + + private final int _max_coord; + private final int[] _xcoord; + private final int[] _ycoord; + + private void normalizeCoord(int[] i_coord_x, int[] i_coord_y, int i_index, int i_coord_num) + { + // vertex1を境界にして、後方に配列を連結 + System.arraycopy(i_coord_x, 1, i_coord_x, i_coord_num, i_index); + System.arraycopy(i_coord_y, 1, i_coord_y, i_coord_num, i_index); + } + + private final int[] __detectMarker_mkvertex = new int[5]; + + /** + * ARMarkerInfo2 *arDetectMarker2( ARInt16 *limage, int label_num, int *label_ref,int *warea, double *wpos, int *wclip,int area_max, int area_min, double + * factor, int *marker_num ) 関数の代替品 ラベリング情報からマーカー一覧を作成してo_marker_listを更新します。 関数はo_marker_listに重なりを除外したマーカーリストを作成します。 + * + * @param i_raster + * 解析する2値ラスタイメージを指定します。 + * @param o_square_stack + * 抽出した正方形候補を格納するリスト + * @throws NyARException + */ + public final void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException + { + final INyARLabeling labeling_proc = this._labeling; + final NyARLabelingImage limage = this._limage; + + // 初期化 + + // マーカーホルダをリセット + o_square_stack.clear(); + + // ラベリング + labeling_proc.labeling(i_raster); + + // ラベル数が0ならここまで + final int label_num = limage.getLabelStack().getLength(); + if (label_num < 1) { + return; + } + + final NyARLabelingLabelStack stack = limage.getLabelStack(); + final NyARLabelingLabel[] labels = (NyARLabelingLabel[])stack.getArray(); + + + // ラベルを大きい順に整列 + stack.sortByArea(); + + // デカいラベルを読み飛ばし + int i; + for (i = 0; i < label_num; i++) { + // 検査対象内のラベルサイズになるまで無視 + if (labels[i].area <= AR_AREA_MAX) { + break; + } + } + + final int xsize = this._width; + final int ysize = this._height; + final int[] xcoord = this._xcoord; + final int[] ycoord = this._ycoord; + final int coord_max = this._max_coord; + final int[] mkvertex = this.__detectMarker_mkvertex; + final OverlapChecker overlap = this._overlap_checker; + int coord_num; + int label_area; + NyARLabelingLabel label_pt; + + //重なりチェッカの最大数を設定 + overlap.reset(label_num); + + for (; i < label_num; i++) { + label_pt = labels[i]; + label_area = label_pt.area; + // 検査対象サイズよりも小さくなったら終了 + if (label_area < AR_AREA_MIN) { + break; + } + // クリップ領域が画面の枠に接していれば除外 + if (label_pt.clip_l == 1 || label_pt.clip_r == xsize - 2) {// if(wclip[i*4+0] == 1 || wclip[i*4+1] ==xsize-2){ + continue; + } + if (label_pt.clip_t == 1 || label_pt.clip_b == ysize - 2) {// if( wclip[i*4+2] == 1 || wclip[i*4+3] ==ysize-2){ + continue; + } + // 既に検出された矩形との重なりを確認 + if (!overlap.check(label_pt)) { + // 重なっているようだ。 + continue; + } + + // 輪郭を取得 + coord_num = limage.getContour(i, coord_max, xcoord, ycoord); + if (coord_num == coord_max) { + // 輪郭が大きすぎる。 + continue; + } + //頂点候補のインデクスを取得 + final int vertex1 = scanVertex(xcoord, ycoord, coord_num); + + // 頂点候補(vertex1)を先頭に並べなおした配列を作成する。 + normalizeCoord(xcoord, ycoord, vertex1, coord_num); + + // 領域を準備する。 + NyARSquare square_ptr = (NyARSquare)o_square_stack.prePush(); + + // 頂点情報を取得 + if (!getSquareVertex(xcoord, ycoord, vertex1, coord_num, label_area, mkvertex)) { + o_square_stack.pop();// 頂点の取得が出来なかったので破棄 + continue; + } + + if (!getSquareLine(mkvertex, xcoord, ycoord, square_ptr)) { + // 矩形が成立しなかった。 + o_square_stack.pop(); + continue; + } + // 検出済の矩形の属したラベルを重なりチェックに追加する。 + overlap.push(label_pt); + } + return; + } + + /** + * 辺からの対角線が最長になる点を対角線候補として返す。 + * + * @param i_xcoord + * @param i_ycoord + * @param i_coord_num + * @return + */ + private int scanVertex(int[] i_xcoord, int[] i_ycoord, int i_coord_num) + { + final int sx = i_xcoord[0]; + final int sy = i_ycoord[0]; + int d = 0; + int w, x, y; + int ret = 0; + for (int i = 1; i < i_coord_num; i++) { + x = i_xcoord[i] - sx; + y = i_ycoord[i] - sy; + w = x * x + y * y; + if (w > d) { + d = w; + ret = i; + } + // ここでうまく終了条件入れられないかな。 + } + return ret; + } + + private final NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter(); + + private final NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter(); + + /** + * static int arDetectMarker2_check_square( int area, ARMarkerInfo2 *marker_info2, double factor ) 関数の代替関数 OPTIMIZED STEP [450->415] o_squareに頂点情報をセットします。 + * + * @param i_x_coord + * @param i_y_coord + * @param i_vertex1_index + * @param i_coord_num + * @param i_area + * @param o_vertex + * 要素数はint[4]である事 + * @return + */ + private boolean getSquareVertex(int[] i_x_coord, int[] i_y_coord, int i_vertex1_index, int i_coord_num, int i_area, int[] o_vertex) + { + final NyARVertexCounter wv1 = this.__getSquareVertex_wv1; + final NyARVertexCounter wv2 = this.__getSquareVertex_wv2; + final int end_of_coord = i_vertex1_index + i_coord_num - 1; + final int sx = i_x_coord[i_vertex1_index];// sx = marker_info2->x_coord[0]; + final int sy = i_y_coord[i_vertex1_index];// sy = marker_info2->y_coord[0]; + int dmax = 0; + int v1 = i_vertex1_index; + for (int i = 1 + i_vertex1_index; i < end_of_coord; i++) {// for(i=1;icoord_num-1;i++) + // { + final int d = (i_x_coord[i] - sx) * (i_x_coord[i] - sx) + (i_y_coord[i] - sy) * (i_y_coord[i] - sy); + if (d > dmax) { + dmax = d; + v1 = i; + } + } + final double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR; + + o_vertex[0] = i_vertex1_index; + + if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v1, thresh)) { // if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,0,v1,thresh,wv1,&wvnum1)< + // 0 ) { + return false; + } + if (!wv2.getVertex(i_x_coord, i_y_coord, v1, end_of_coord, thresh)) {// if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,v1,marker_info2->coord_num-1,thresh,wv2,&wvnum2) + // < 0) { + return false; + } + + int v2; + if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {// if(wvnum1 == 1 && wvnum2== 1) { + o_vertex[1] = wv1.vertex[0]; + o_vertex[2] = v1; + o_vertex[3] = wv2.vertex[0]; + } else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0) {// }else if( wvnum1 > 1 && wvnum2== 0) { + //頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。 + v2 = (v1-i_vertex1_index)/2+i_vertex1_index; + if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v2, thresh)) { + return false; + } + if (!wv2.getVertex(i_x_coord, i_y_coord, v2, v1, thresh)) { + return false; + } + if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) { + o_vertex[1] = wv1.vertex[0]; + o_vertex[2] = wv2.vertex[0]; + o_vertex[3] = v1; + } else { + return false; + } + } else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1) { + //v2 = (v1-i_vertex1_index+ end_of_coord-i_vertex1_index) / 2+i_vertex1_index; + v2 = (v1+ end_of_coord)/2; + + if (!wv1.getVertex(i_x_coord, i_y_coord, v1, v2, thresh)) { + return false; + } + if (!wv2.getVertex(i_x_coord, i_y_coord, v2, end_of_coord, thresh)) { + return false; + } + if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) { + o_vertex[1] = v1; + o_vertex[2] = wv1.vertex[0]; + o_vertex[3] = wv2.vertex[0]; + } else { + return false; + } + } else { + return false; + } + o_vertex[4] = end_of_coord; + return true; + } + + private final NyARMat __getSquareLine_input = new NyARMat(1, 2); + + private final NyARMat __getSquareLine_evec = new NyARMat(2, 2); + + private final NyARVec __getSquareLine_ev = new NyARVec(2); + + private final NyARVec __getSquareLine_mean = new NyARVec(2); + + /** + * arGetLine(int x_coord[], int y_coord[], int coord_num,int vertex[], double line[4][3], double v[4][2]) arGetLine2(int x_coord[], int y_coord[], int + * coord_num,int vertex[], double line[4][3], double v[4][2], double *dist_factor) の2関数の合成品です。 マーカーのvertex,lineを計算して、結果をo_squareに保管します。 + * Optimize:STEP[424->391] + * + * @param i_cparam + * @return + * @throws NyARException + */ + private boolean getSquareLine(int[] i_mkvertex, int[] i_xcoord, int[] i_ycoord, NyARSquare o_square) throws NyARException + { + final NyARLinear[] l_line = o_square.line; + final NyARVec ev = this.__getSquareLine_ev; // matrixPCAの戻り値を受け取る + final NyARVec mean = this.__getSquareLine_mean;// matrixPCAの戻り値を受け取る + final double[] mean_array = mean.getArray(); + final NyARCameraDistortionFactor dist_factor=this._dist_factor_ref; + final NyARMat input = this.__getSquareLine_input;// 次処理で初期化される。 + final NyARMat evec = this.__getSquareLine_evec;// アウトパラメータを受け取るから初期化不要//new NyARMat(2,2); + final double[][] evec_array = evec.getArray(); + for (int i = 0; i < 4; i++) { + final double w1 = (double) (i_mkvertex[i + 1] - i_mkvertex[i] + 1) * 0.05 + 0.5; + final int st = (int) (i_mkvertex[i] + w1); + final int ed = (int) (i_mkvertex[i + 1] - w1); + final int n = ed - st + 1; + if (n < 2) { + // nが2以下でmatrix.PCAを計算することはできないので、エラー + return false; + } + // pcaの準備 + input.realloc(n, 2); + // バッチ取得 + dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st, n, input.getArray()); + + // 主成分分析 + input.matrixPCA(evec, ev, mean); + final NyARLinear l_line_i = l_line[i]; + l_line_i.run = evec_array[0][1];// line[i][0] = evec->m[1]; + l_line_i.rise = -evec_array[0][0];// line[i][1] = -evec->m[0]; + l_line_i.intercept = -(l_line_i.run * mean_array[0] + l_line_i.rise * mean_array[1]);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]); + } + + final NyARDoublePoint2d[] l_sqvertex = o_square.sqvertex; + final NyARIntPoint[] l_imvertex = o_square.imvertex; + for (int i = 0; i < 4; i++) { + final NyARLinear l_line_i = l_line[i]; + final NyARLinear l_line_2 = l_line[(i + 3) % 4]; + final double w1 = l_line_2.run * l_line_i.rise - l_line_i.run * l_line_2.rise; + if (w1 == 0.0) { + return false; + } + l_sqvertex[i].x = (l_line_2.rise * l_line_i.intercept - l_line_i.rise * l_line_2.intercept) / w1; + l_sqvertex[i].y = (l_line_i.run * l_line_2.intercept - l_line_2.run * l_line_i.intercept) / w1; + // 頂点インデクスから頂点座標を得て保存 + l_imvertex[i].x = i_xcoord[i_mkvertex[i]]; + l_imvertex[i].y = i_ycoord[i_mkvertex[i]]; + } + return true; + } +} + +/** + * get_vertex関数を切り離すためのクラス + * + */ +final class NyARVertexCounter +{ + public final int[] vertex = new int[10];// 5まで削れる + + public int number_of_vertex; + + private double thresh; + + private int[] x_coord; + + private int[] y_coord; + + public boolean getVertex(int[] i_x_coord, int[] i_y_coord, int st, int ed, double i_thresh) + { + this.number_of_vertex = 0; + this.thresh = i_thresh; + this.x_coord = i_x_coord; + this.y_coord = i_y_coord; + return get_vertex(st, ed); + } + + /** + * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数 + * + * @param x_coord + * @param y_coord + * @param st + * @param ed + * @param thresh + * @return + */ + private boolean get_vertex(int st, int ed) + { + int v1 = 0; + final int[] lx_coord = this.x_coord; + final int[] ly_coord = this.y_coord; + final double a = ly_coord[ed] - ly_coord[st]; + final double b = lx_coord[st] - lx_coord[ed]; + final double c = lx_coord[ed] * ly_coord[st] - ly_coord[ed] * lx_coord[st]; + double dmax = 0; + for (int i = st + 1; i < ed; i++) { + final double d = a * lx_coord[i] + b * ly_coord[i] + c; + if (d * d > dmax) { + dmax = d * d; + v1 = i; + } + } + if (dmax / (a * a + b * b) > thresh) { + if (!get_vertex(st, v1)) { + return false; + } + if (number_of_vertex > 5) { + return false; + } + vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1; + number_of_vertex++;// (*vnum)++; + + if (!get_vertex(v1, ed)) { + return false; + } + } + return true; + } +} + +/** + * ラベル同士の重なり(内包関係)を調べるクラスです。 ラベルリストに内包するラベルを蓄積し、それにターゲットのラベルが内包されているか を確認します。 + */ +class OverlapChecker +{ + private NyARLabelingLabel[] _labels = new NyARLabelingLabel[32]; + + private int _length; + + /** + * 最大i_max_label個のラベルを蓄積できるようにオブジェクトをリセットする + * + * @param i_max_label + */ + public void reset(int i_max_label) + { + if (i_max_label > this._labels.length) { + this._labels = new NyARLabelingLabel[i_max_label]; + } + this._length = 0; + } + + /** + * チェック対象のラベルを追加する。 + * + * @param i_label_ref + */ + public void push(NyARLabelingLabel i_label_ref) + { + this._labels[this._length] = i_label_ref; + this._length++; + } + + /** + * 現在リストにあるラベルと重なっているかを返す。 + * + * @param i_label + * @return 何れかのラベルの内側にあるならばfalse,独立したラベルである可能性が高ければtrueです. + */ + public boolean check(NyARLabelingLabel i_label) + { + // 重なり処理かな? + final NyARLabelingLabel[] label_pt = this._labels; + final int px1 = (int) i_label.pos_x; + final int py1 = (int) i_label.pos_y; + for (int i = this._length - 1; i >= 0; i--) { + final int px2 = (int) label_pt[i].pos_x; + final int py2 = (int) label_pt[i].pos_y; + final int d = (px1 - px2) * (px1 - px2) + (py1 - py2) * (py1 - py2); + if (d < label_pt[i].area / 4) { + // 対象外 + return false; + } + } + // 対象 + return true; + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquareStack.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquareStack.java new file mode 100644 index 0000000..a88109a --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARSquareStack.java @@ -0,0 +1,51 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.utils.*; + +public class NyARSquareStack extends NyObjectStack +{ + public NyARSquareStack(int i_length) + { + super(new NyARSquare[i_length]); + + } + + protected void onReservRequest(int i_start, int i_end, Object[] i_buffer) + { + for (int i = i_start; i < i_end; i++) { + i_buffer[i] = new NyARSquare(); + } + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARVec.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARVec.java new file mode 100644 index 0000000..8bc84d7 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARVec.java @@ -0,0 +1,285 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core; + +import jp.nyatla.nyartoolkit.NyARException; + + + +public class NyARVec +{ + private int clm; + + public NyARVec(int i_clm) + { + v = new double[i_clm]; + clm = i_clm; + } + + private double[] v; + + /** + * i_clmサイズの列を格納できるように列サイズを変更します。 実行後、列の各値は不定になります。 + * + * @param i_clm + */ + public void realloc(int i_clm) + { + if (i_clm <= this.v.length) { + // 十分な配列があれば何もしない。 + } else { + // 不十分なら取り直す。 + v = new double[i_clm]; + } + this.clm = i_clm; + } + + public int getClm() + { + return clm; + } + + public double[] getArray() + { + return v; + } + + /** + * arVecDispの代替品 + * + * @param value + * @return + */ + public int arVecDisp() throws NyARException + { + NyARException.trap("未チェックのパス"); + System.out.println(" === vector (" + clm + ") ===\n");// printf(" === + // vector (%d) + // ===\n", + // v->clm); + System.out.print(" |");// printf(" |"); + for (int c = 0; c < clm; c++) {// for( c = 0; c < v->clm; c++ ){ + System.out.print(" " + v[c]);// printf( " %10g", v->v[c] ); + } + System.out.println(" |");// printf(" |\n"); + System.out.println(" ===================");// printf(" + // ===================\n"); + return 0; + } + + /** + * arVecInnerproduct関数の代替品 + * + * @param x + * @param y + * @param i_start + * 演算開始列(よくわからないけどarVecTridiagonalizeの呼び出し元でなんかしてる) + * @return + * @throws NyARException + */ + public double vecInnerproduct(NyARVec y, int i_start) throws NyARException + { + NyARException.trap("この関数は動作確認できていません。"); + double result = 0.0; + // double[] x_array=x.v;.getArray(); + // double[] y_array=y.getArray(); + + if (this.clm != y.clm) { + throw new NyARException();// exit(); + } + for (int i = i_start; i < this.clm; i++) { + NyARException.trap("未チェックのパス"); + result += this.v[i] * y.v[i];// result += x->v[i] * y->v[i]; + } + return result; + } + + /** + * double arVecHousehold関数の代替品 + * + * @param x + * @param i_start + * 演算開始列(よくわからないけどarVecTridiagonalizeの呼び出し元でなんかしてる) + * @return + * @throws NyARException + */ + public double vecHousehold(int i_start) throws NyARException + { + NyARException.trap("この関数は動作確認できていません。"); + double s, t; + s = Math.sqrt(this.vecInnerproduct(this, i_start)); + // double[] x_array=x.getArray(); + if (s != 0.0) { + NyARException.trap("未チェックのパス"); + if (this.v[i_start] < 0) { + s = -s; + } + NyARException.trap("未チェックのパス"); + { + this.v[i_start] += s;// x->v[0] += s; + t = 1 / Math.sqrt(this.v[i_start] * s);// t = 1 / sqrt(x->v[0] * s); + } + for (int i = i_start; i < this.clm; i++) { + NyARException.trap("未チェックのパス"); + this.v[i] *= t;// x->v[i] *= t; + } + } + return -s; + } + + // /** + // * arVecTridiagonalize関数の代替品 + // * a,d,e間で演算をしてる。何をどうしているかはさっぱりさっぱり + // * @param a + // * @param d + // * @param e + // * @param i_e_start + // * 演算開始列(よくわからないけどarVecTridiagonalizeの呼び出し元でなんかしてる) + // * @return + // * @throws NyARException + // */ + // public static void vecTridiagonalize(NyARMat a, NyARVec d, NyARVec e,int + // i_e_start) throws NyARException + // { + // NyARVec vec,vec2; + // double[][] a_array=a.getArray(); + // double s, t, p, q; + // int dim; + // + // if(a.getClm()!=a.getRow()){ + // throw new NyARException(); + // } + // if(a.getClm() != d.clm){ + // throw new NyARException(); + // } + // if(a.getClm() != e.clm){ + // throw new NyARException(); + // } + // dim = a.getClm(); + // + // for(int k = 0; k < dim-2; k++ ){ + // vec=a.getRowVec(k); + // // double[] vec_array=vec.getArray(); + // NyARException.trap("未チェックパス"); + // d.v[k]=vec.v[k];//d.set(k,v.get(k)); //d->v[k] = v[k]; + // + // //wv1.clm = dim-k-1; + // //wv1.v = &(v[k+1]); + // NyARException.trap("未チェックパス"); + // e.v[k+i_e_start]=vec.vecHousehold(k+1);//e->v[k] = arVecHousehold(&wv1); + // if(e.v[k+i_e_start]== 0.0 ){ + // continue; + // } + // + // for(int i = k+1; i < dim; i++ ){ + // s = 0.0; + // for(int j = k+1; j < i; j++ ) { + // NyARException.trap("未チェックのパス"); + // s += a_array[j][i] * vec.v[j];//s += a.get(j*dim+i) * v.get(j);//s += + // a->m[j*dim+i] * v[j]; + // } + // for(int j = i; j < dim; j++ ) { + // NyARException.trap("未チェックのパス"); + // s += a_array[i][j] * vec.v[j];//s += a.get(i*dim+j) * v.get(j);//s += + // a->m[i*dim+j] * v[j]; + // } + // NyARException.trap("未チェックのパス"); + // d.v[i]=s;//d->v[i] = s; + // } + // + // + // //wv1.clm = wv2.clm = dim-k-1; + // //wv1.v = &(v[k+1]); + // //wv2.v = &(d->v[k+1]); + // vec=a.getRowVec(k); + // // vec_array=vec.getArray(); + // NyARException.trap("未チェックパス"); + // t = vec.vecInnerproduct(d,k+1)/ 2; + // for(int i = dim-1; i > k; i-- ) { + // NyARException.trap("未チェックパス"); + // p = vec.v[i];//p = v.get(i);//p = v[i]; + // d.v[i]-=t*p;q=d.v[i];//q = d->v[i] -= t*p; + // for(int j = i; j < dim; j++ ){ + // NyARException.trap("未チェックパス"); + // a_array[i][j]-=p*(d.v[j] + q*vec.v[j]);//a->m[i*dim+j] -= p*(d->v[j]) + + // q*v[j]; + // } + // } + // } + // + // if( dim >= 2) { + // d.v[dim-2]=a_array[dim-2][dim-2];//d->v[dim-2] = + // a->m[(dim-2)*dim+(dim-2)]; + // e.v[dim-2+i_e_start]=a_array[dim-2][dim-1];//e->v[dim-2] = + // a->m[(dim-2)*dim+(dim-1)]; + // } + // + // if( dim >= 1 ){ + // d.v[dim-1]=a_array[dim-1][dim-1];//d->v[dim-1] = + // a->m[(dim-1)*dim+(dim-1)]; + // } + // + // for(int k = dim-1; k >= 0; k--) { + // vec=a.getRowVec(k);//v = a.getPointer(k*dim);//v = &(a->m[k*dim]); + // if( k < dim-2 ) { + // for(int i = k+1; i < dim; i++ ){ + // //wv1.clm = wv2.clm = dim-k-1; + // //wv1.v = &(v[k+1]); + // //wv2.v = &(a->m[i*dim+k+1]); + // vec2=a.getRowVec(i); + // + // t = vec.vecInnerproduct(vec2,k+1); + // for(int j = k+1; j < dim; j++ ){ + // NyARException.trap("未チェックパス"); + // a_array[i][j]-=t*vec.v[j];//a.subValue(i*dim+j,t*v.get(j));//a->m[i*dim+j] + // -= t * v[j]; + // } + // } + // } + // for(int i = 0; i < dim; i++ ){ + // vec.v[i]=0.0;//v.set(i,0.0);//v[i] = 0.0; + // } + // vec.v[k]=1;//v.set(k,1);//v[k] = 1; + // } + // } + /** + * 現在ラップしている配列を取り外して、新しい配列をラップします。 + * + * @param i_v + * @param i_clm + */ + public void setNewArray(double[] i_array, int i_clm) + { + this.v = i_array; + this.clm = i_clm; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARVersion.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARVersion.java new file mode 100644 index 0000000..92497ba --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/NyARVersion.java @@ -0,0 +1,74 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core; + +/** + * ARUint32 arGetVersion(char **versionStringRef); 関数の置き換え + */ +public class NyARVersion +{ + private static final int AR_HEADER_VERSION_MAJOR = 2; // #define + // AR_HEADER_VERSION_MAJOR + // 2 + + private static final int AR_HEADER_VERSION_MINOR = 72;// #define AR_HEADER_VERSION_MINOR 72 + + private static final int AR_HEADER_VERSION_TINY = 0;// #define AR_HEADER_VERSION_TINY 0 + + private static final int AR_HEADER_VERSION_BUILD = 0;// #define AR_HEADER_VERSION_BUILD 0 + + private static final String AR_HEADER_VERSION_STRING = "2.72.0";// #define AR_HEADER_VERSION_STRING "2.72.0" + + public static final boolean AR_HAVE_HEADER_VERSION_2 = true;// #define AR_HAVE_HEADER_VERSION_2 + + public static final boolean AR_HAVE_HEADER_VERSION_2_72 = true;// #define AR_HAVE_HEADER_VERSION_2_72 + + public static String getARVersion() + { + return AR_HEADER_VERSION_STRING; + } + + public static int getARVersionInt() + { + // Represent full version number (major, minor, tiny, build) in + // binary coded decimal. N.B: Integer division. + return (int) (0x10000000 * (AR_HEADER_VERSION_MAJOR / 10)) + + (int) (0x01000000 * (AR_HEADER_VERSION_MAJOR % 10)) + + (int) (0x00100000 * (AR_HEADER_VERSION_MINOR / 10)) + + (int) (0x00010000 * (AR_HEADER_VERSION_MINOR % 10)) + + (int) (0x00001000 * (AR_HEADER_VERSION_TINY / 10)) + + (int) (0x00000100 * (AR_HEADER_VERSION_TINY % 10)) + + (int) (0x00000010 * (AR_HEADER_VERSION_BUILD / 10)) + + (int) (0x00000001 * (AR_HEADER_VERSION_BUILD % 10)); + + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/INyARLabeling.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/INyARLabeling.java new file mode 100644 index 0000000..42d885f --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/INyARLabeling.java @@ -0,0 +1,43 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.labeling; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; + +public interface INyARLabeling +{ + public void attachDestination(INyARLabelingImage i_destination_image) throws NyARException; + public INyARLabelingImage getAttachedDestination(); + + public void labeling(NyARBinRaster i_raster) throws NyARException; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/INyARLabelingImage.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/INyARLabelingImage.java new file mode 100644 index 0000000..8d66292 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/INyARLabelingImage.java @@ -0,0 +1,40 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.labeling; +import jp.nyatla.nyartoolkit.core.raster.*; + +public interface INyARLabelingImage extends INyARRaster +{ + public int[] getIndexArray(); + public NyARLabelingLabelStack getLabelStack(); + public void reset(boolean i_label_index_enable); +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingImage.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingImage.java new file mode 100644 index 0000000..fb8d096 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingImage.java @@ -0,0 +1,181 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.labeling; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.rasterreader.NyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * + */ +public class NyARLabelingImage extends NyARRaster_BasicClass implements INyARLabelingImage +{ + private final static int MAX_LABELS = 1024*32; + protected int[][] _ref_buf; + private INyARBufferReader _buffer_reader; + protected NyARLabelingLabelStack _label_list; + protected int[] _index_table; + protected boolean _is_index_table_enable; + public NyARLabelingImage(int i_width, int i_height) + { + super(new NyARIntSize(i_width,i_height)); + this._ref_buf =new int[i_height][i_width]; + this._label_list = new NyARLabelingLabelStack(MAX_LABELS); + this._index_table=new int[MAX_LABELS]; + this._is_index_table_enable=false; + this._buffer_reader=new NyARBufferReader(this._ref_buf,INyARBufferReader.BUFFERFORMAT_INT2D); + + return; + } + public INyARBufferReader getBufferReader() + { + return this._buffer_reader; + } + + + /** + * ラベリング結果がインデックステーブルを持つ場合、その配列を返します。 + * 持たない場合、nullを返します。 + * + * 値がnullの時はラベル番号そのものがラスタに格納されていますが、 + * null以外の時はラスタに格納されているのはインデクス番号です。 + * + * インデクス番号とラベル番号の関係は、以下の式で表されます。 + * ラベル番号:=value[インデクス番号] + * + */ + public int[] getIndexArray() + { + return this._is_index_table_enable?this._index_table:null; + } + + public NyARLabelingLabelStack getLabelStack() + { + return this._label_list; + } + public void reset(boolean i_label_index_enable) + { + assert(i_label_index_enable==true);//非ラベルモードは未実装 + this._label_list.clear(); + this._is_index_table_enable=i_label_index_enable; + return; + } + + protected final int[] _getContour_xdir = { 0, 1, 1, 1, 0,-1,-1,-1}; + protected final int[] _getContour_ydir = {-1,-1, 0, 1, 1, 1, 0,-1}; + /** + * i_labelのラベルの、クリップ領域が上辺に接しているx座標を返します。 + * @param i_index + * @return + */ + protected int getTopClipTangentX(NyARLabelingLabel i_label) throws NyARException + { + int w; + int i_label_id=i_label.id; + int[] index_table=this._index_table; + int[] limage_j=this._ref_buf[i_label.clip_t]; + final int clip1 = i_label.clip_r; + // p1=ShortPointer.wrap(limage,j*xsize+clip.get());//p1 =&(limage[j*xsize+clip[0]]); + for (int i = i_label.clip_l; i <= clip1; i++) {// for( i = clip[0]; i <=clip[1]; i++, p1++ ) { + w = limage_j[i]; + if (w > 0 && index_table[w-1] == i_label_id){ + return i; + } + } + //あれ?見つからないよ? + throw new NyARException(); + } + /** + * i_index番目のラベルの輪郭線を配列に返します。 + * @param i_index + * @param i_array_size + * @param o_coord_x + * @param o_coord_y + * @return + * 輪郭線の長さを返します。 + * @throws NyARException + */ + public int getContour(int i_index,int i_array_size,int[] o_coord_x,int[] o_coord_y) throws NyARException + { + final int[] xdir = this._getContour_xdir;// static int xdir[8] = { 0,1, 1, 1, 0,-1,-1,-1}; + final int[] ydir = this._getContour_ydir;// static int ydir[8] = {-1,-1,0, 1, 1, 1, 0,-1}; + final NyARLabelingLabel label=(NyARLabelingLabel)this._label_list.getItem(i_index); + int i; + //クリップ領域の上端に接しているポイントを得る。 + int sx=getTopClipTangentX(label); + int sy=label.clip_t; + + int coord_num = 1;// marker_info2->coord_num = 1; + o_coord_x[0] = sx;// marker_info2->x_coord[0] = sx; + o_coord_y[0] = sy;// marker_info2->y_coord[0] = sy; + int dir = 5; + + int[][] limage=this._ref_buf; + int c = o_coord_x[0]; + int r = o_coord_y[0]; + for (;;) { + dir = (dir + 5) % 8; + for (i = 0; i < 8; i++) { + if (limage[r + ydir[dir]][c + xdir[dir]] > 0) {// if( + // p1[ydir[dir]*xsize+xdir[dir]] > 0 ){ + break; + } + dir = (dir + 1) % 8; + } + if (i == 8) { + //8方向全て調べたけどラベルが無いよ? + throw new NyARException();// return(-1); + } + // xcoordとycoordをc,rにも保存 + c = c + xdir[dir];// marker_info2->x_coord[marker_info2->coord_num]=marker_info2->x_coord[marker_info2->coord_num-1] + // + xdir[dir]; + r = r + ydir[dir];// marker_info2->y_coord[marker_info2->coord_num]=marker_info2->y_coord[marker_info2->coord_num-1]+ ydir[dir]; + o_coord_x[coord_num] = c;// marker_info2->x_coord[marker_info2->coord_num]=marker_info2->x_coord[marker_info2->coord_num-1]+ xdir[dir]; + o_coord_y[coord_num] = r;// marker_info2->y_coord[marker_info2->coord_num]=marker_info2->y_coord[marker_info2->coord_num-1]+ ydir[dir]; + // 終了条件判定 + if (c == sx && r == sy){ + coord_num++; + break; + } + coord_num++; + if (coord_num == i_array_size) {// if( marker_info2.coord_num ==Config.AR_CHAIN_MAX-1 ){ + //輪郭が末端に達した + return coord_num; + } + } + return coord_num; + + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingLabel.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingLabel.java new file mode 100644 index 0000000..28e4ef1 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingLabel.java @@ -0,0 +1,44 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.labeling; + +public class NyARLabelingLabel +{ + public int id; + public int area; + public int clip_r;// 0 + public int clip_l;// 1 + public int clip_b;// 2 + public int clip_t;// 3 + public double pos_x; + public double pos_y; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingLabelStack.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingLabelStack.java new file mode 100644 index 0000000..0ed1c0b --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabelingLabelStack.java @@ -0,0 +1,87 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.labeling; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.utils.*; + +/** + * NyLabelの予約型動的配列 + * + */ +public class NyARLabelingLabelStack extends NyObjectStack +{ + protected NyARLabelingLabelStack(NyARLabelingLabel[] i_label_array) + { + super(i_label_array); + } + public NyARLabelingLabelStack(int i_max_array_size) + { + super(new NyARLabelingLabel[i_max_array_size]); + } + + protected void onReservRequest(int i_start, int i_end, Object[] i_buffer) + { + for (int i = i_start; i < i_end; i++) { + i_buffer[i] = new NyARLabelingLabel(); + } + } + + /** + * エリアの大きい順にラベルをソートします。 + */ + final public void sortByArea() + { + int len=this._length; + int h = len *13/10; + NyARLabelingLabel[] item=(NyARLabelingLabel[])this._items; + for(;;){ + int swaps = 0; + for (int i = 0; i + h < len; i++) { + if (item[i + h].area > item[i].area) { + final NyARLabelingLabel temp = item[i + h]; + item[i + h] = item[i]; + item[i] = temp; + swaps++; + } + } + if (h == 1) { + if (swaps == 0){ + break; + } + }else{ + h=h*10/13; + } + } + } +} + diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabeling_ARToolKit.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabeling_ARToolKit.java new file mode 100644 index 0000000..76bf91e --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/labeling/NyARLabeling_ARToolKit.java @@ -0,0 +1,353 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.labeling; + + + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * ARToolKit互換のラベリングクラスです。 ARToolKitと同一な評価結果を返します。 + * + */ +public class NyARLabeling_ARToolKit implements INyARLabeling +{ + private static final int WORK_SIZE = 1024 * 32;// #define WORK_SIZE 1024*32 + private final NyARWorkHolder work_holder = new NyARWorkHolder(WORK_SIZE); + private NyARIntSize _dest_size; + private INyARLabelingImage _out_image; + + public void attachDestination(INyARLabelingImage i_destination_image)throws NyARException + { + // サイズチェック + NyARIntSize size = i_destination_image.getSize(); + this._out_image = i_destination_image; + + // NyLabelingImageのイメージ初期化(枠書き) + int[][] img = (int[][])i_destination_image.getBufferReader().getBuffer(); + for (int i = 0; i < size.w; i++) { + img[0][i] = 0; + img[size.h - 1][i] = 0; + } + for (int i = 0; i < size.h; i++) { + img[i][0] = 0; + img[i][size.w - 1] = 0; + } + + // サイズ(参照値)を保存 + this._dest_size = size; + } + public INyARLabelingImage getAttachedDestination() + { + return this._out_image; + } + /** + * static ARInt16 *labeling2( ARUint8 *image, int thresh,int *label_num, int **area, double **pos, int **clip,int **label_ref, int LorR ) 関数の代替品 + * ラスタimageをラベリングして、結果を保存します。 Optimize:STEP[1514->1493] + * + * @param i_raster + * @throws NyARException + */ + public void labeling(NyARBinRaster i_raster) throws NyARException + { + int m, n; /* work */ + int i, j, k; + INyARLabelingImage out_image = this._out_image; + + // サイズチェック + NyARIntSize in_size = i_raster.getSize(); + this._dest_size.isEqualSize(in_size); + + final int lxsize = in_size.w;// lxsize = arUtil_c.arImXsize; + final int lysize = in_size.h;// lysize = arUtil_c.arImYsize; + int[][] label_img = (int[][])out_image.getBufferReader().getBuffer(); + + // 枠作成はインスタンスを作った直後にやってしまう。 + + //ラベリング情報のリセット(ラベリングインデックスを使用) + out_image.reset(true); + + int[] label_idxtbl=out_image.getIndexArray(); + + int[] work2_pt; + int wk_max = 0; + + int label_pixel; + int[][] raster_buf=(int[][])i_raster.getBufferReader().getBuffer(); + int[] line_ptr; + int[][] work2 = this.work_holder.work2; + int[] label_img_pt0, label_img_pt1; + for (j = 1; j < lysize - 1; j++) {// for (int j = 1; j < lysize - 1;j++, pnt += poff*2, pnt2 += 2) { + line_ptr=raster_buf[j]; + label_img_pt0 = label_img[j]; + label_img_pt1 = label_img[j - 1]; + for (i = 1; i < lxsize - 1; i++) {// for(int i = 1; i < lxsize-1;i++, pnt+=poff, pnt2++) { + // RGBの合計値が閾値より小さいかな? + if (line_ptr[i]==0) { + // pnt1 = ShortPointer.wrap(pnt2, -lxsize);//pnt1 =&(pnt2[-lxsize]); + if (label_img_pt1[i] > 0) {// if( *pnt1 > 0 ) { + label_pixel = label_img_pt1[i];// *pnt2 = *pnt1; + + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + work2_pt[6] = j;// work2[((*pnt2)-1)*7+6] = j; + } else if (label_img_pt1[i + 1] > 0) {// }else if(*(pnt1+1) > 0 ) { + if (label_img_pt1[i - 1] > 0) {// if( *(pnt1-1) > 0 ) { + m = label_idxtbl[label_img_pt1[i + 1] - 1];// m =work[*(pnt1+1)-1]; + n = label_idxtbl[label_img_pt1[i - 1] - 1];// n =work[*(pnt1-1)-1]; + if (m > n) { + label_pixel = n;// *pnt2 = n; + // wk=IntPointer.wrap(work, 0);//wk = + // &(work[0]); + for (k = 0; k < wk_max; k++) { + if (label_idxtbl[k] == m) {// if( *wk == m ) + label_idxtbl[k] = n;// *wk = n; + } + } + } else if (m < n) { + label_pixel = m;// *pnt2 = m; + // wk=IntPointer.wrap(work,0);//wk = &(work[0]); + for (k = 0; k < wk_max; k++) { + if (label_idxtbl[k] == n) {// if( *wk == n ){ + label_idxtbl[k] = m;// *wk = m; + } + } + } else { + label_pixel = m;// *pnt2 = m; + } + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++; + work2_pt[1] += i; + work2_pt[2] += j; + work2_pt[6] = j; + } else if ((label_img_pt0[i - 1]) > 0) {// }else if(*(pnt2-1) > 0) { + m = label_idxtbl[(label_img_pt1[i + 1]) - 1];// m =work[*(pnt1+1)-1]; + n = label_idxtbl[label_img_pt0[i - 1] - 1];// n =work[*(pnt2-1)-1]; + if (m > n) { + + label_pixel = n;// *pnt2 = n; + for (k = 0; k < wk_max; k++) { + if (label_idxtbl[k] == m) {// if( *wk == m ){ + label_idxtbl[k] = n;// *wk = n; + } + } + } else if (m < n) { + label_pixel = m;// *pnt2 = m; + for (k = 0; k < wk_max; k++) { + if (label_idxtbl[k] == n) {// if( *wk == n ){ + label_idxtbl[k] = m;// *wk = m; + } + } + } else { + label_pixel = m;// *pnt2 = m; + } + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + } else { + + label_pixel = label_img_pt1[i + 1];// *pnt2 = + // *(pnt1+1); + + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + if (work2_pt[3] > i) {// if( + // work2[((*pnt2)-1)*7+3] > + // i ){ + work2_pt[3] = i;// work2[((*pnt2)-1)*7+3] = i; + } + work2_pt[6] = j;// work2[((*pnt2)-1)*7+6] = j; + } + } else if ((label_img_pt1[i - 1]) > 0) {// }else if( + // *(pnt1-1) > 0 ) { + label_pixel = label_img_pt1[i - 1];// *pnt2 = + // *(pnt1-1); + + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + if (work2_pt[4] < i) {// if( work2[((*pnt2)-1)*7+4] 0) {// }else if(*(pnt2-1) > 0) { + label_pixel = label_img_pt0[i - 1];// *pnt2 =*(pnt2-1); + + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + if (work2_pt[4] < i) {// if( work2[((*pnt2)-1)*7+4] work2_pt[3]) { + label_pt.clip_l = work2_pt[3]; + } + if (label_pt.clip_r < work2_pt[4]) { + label_pt.clip_r = work2_pt[4]; + } + if (label_pt.clip_t > work2_pt[5]) { + label_pt.clip_t = work2_pt[5]; + } + if (label_pt.clip_b < work2_pt[6]) { + label_pt.clip_b = work2_pt[6]; + } + } + + for (i = 0; i < wlabel_num; i++) {// for(int i = 0; i < *label_num; i++ ) { + label_pt = labels[i]; + label_pt.pos_x /= label_pt.area; + label_pt.pos_y /= label_pt.area; + } + return; + } + +} + +/** + * NyARLabeling_O2のworkとwork2を可変長にするためのクラス + * + * + */ +final class NyARWorkHolder +{ + private final static int ARRAY_APPEND_STEP = 256; + + public final int[][] work2; + + private int allocate_size; + + /** + * 最大i_holder_size個の動的割り当てバッファを準備する。 + * + * @param i_holder_size + */ + public NyARWorkHolder(int i_holder_size) + { + // ポインタだけははじめに確保しておく + this.work2 = new int[i_holder_size][]; + this.allocate_size = 0; + } + + /** + * i_indexで指定した番号までのバッファを準備する。 + * + * @param i_index + */ + public final void reserv(int i_index) throws NyARException + { + // アロケート済みなら即リターン + if (this.allocate_size > i_index) { + return; + } + // 要求されたインデクスは範囲外 + if (i_index >= this.work2.length) { + throw new NyARException(); + } + // 追加アロケート範囲を計算 + int range = i_index + ARRAY_APPEND_STEP; + if (range >= this.work2.length) { + range = this.work2.length; + } + // アロケート + for (int i = this.allocate_size; i < range; i++) { + this.work2[i] = new int[7]; + } + this.allocate_size = range; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/INyARMatchPatt.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/INyARMatchPatt.java new file mode 100644 index 0000000..4d53e5b --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/INyARMatchPatt.java @@ -0,0 +1,52 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.match; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARCode; +import jp.nyatla.nyartoolkit.core.pickup.INyARColorPatt; + +/** + * ARColorPattのマッチング計算をするインタフェイスです。 基準Patに対して、計算済みのARCodeデータとの間で比較演算をします。 + * pattern_match関数を分解した3種類のパターン検出クラスを定義します。 + * + */ +public interface INyARMatchPatt +{ + public double getConfidence(); + + public int getDirection(); + + public void evaluate(NyARCode i_code); + + public boolean setPatt(INyARColorPatt i_target_patt) throws NyARException; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_BlackWhite.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_BlackWhite.java new file mode 100644 index 0000000..1073196 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_BlackWhite.java @@ -0,0 +1,133 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.match; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.pickup.INyARColorPatt; + +/** + * AR_TEMPLATE_MATCHING_BWと同等のルールで マーカーを評価します。 + * + */ +public class NyARMatchPatt_BlackWhite implements INyARMatchPatt +{ + private double datapow; + + private int width; + + private int height; + + private double cf = 0; + + private int dir = 0; + + private int ave; + + private int[][][] input = new int[height][width][3]; + + public boolean setPatt(INyARColorPatt i_target_patt) throws NyARException + { + width = i_target_patt.getWidth(); + height = i_target_patt.getHeight(); + int[][][] data = i_target_patt.getPatArray(); + input = new int[height][width][3]; + + int sum = ave = 0; + for (int i = 0; i < height; i++) {// for(int + // i=0;i max) { + max = sum2; + res = j; + } + } + dir = res; + cf = max; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITHOUT_PCA.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITHOUT_PCA.java new file mode 100644 index 0000000..358a4ec --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITHOUT_PCA.java @@ -0,0 +1,185 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.match; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.pickup.INyARColorPatt; + +/** + * AR_TEMPLATE_MATCHING_COLORかつAR_MATCHING_WITHOUT_PCAと同等のルールで マーカーを評価します。 + * + */ +public class NyARMatchPatt_Color_WITHOUT_PCA implements INyARMatchPatt +{ + private int[][][] input = new int[1][1][3]; + + private double datapow; + + private int width = 1; + + private int height = 1; + + private double cf = 0; + + private int dir = 0; + + public double getConfidence() + { + return cf; + } + + public int getDirection() + { + return dir; + } + + /** + * input配列サイズを必要に応じて再アロケートする。 + * + * @param i_width + * @param i_height + */ + private void reallocInputArray(int i_width, int i_height) + { + if (this.input.length < i_height || this.input[0].length < i_width) { + // 配列が十分なサイズでなければ取り直す + this.input = new int[i_height][i_width][3]; + } + this.height = i_height; + this.width = i_width; + } + + public boolean setPatt(INyARColorPatt i_target_patt) throws NyARException + { + int i, k; + int[][][] data, linput; + + // input配列のサイズとwhも更新// input=new int[height][width][3]; + reallocInputArray(i_target_patt.getWidth(), i_target_patt.getHeight()); + int lwidth = this.width; + int lheight = this.height; + linput = this.input; + data = i_target_patt.getPatArray(); + + int sum = 0, l_ave = 0, w_sum; + int[][] data_i, input_i; + int[] data_i_k, input_i_k; + for (i = lheight - 1; i >= 0; i--) {// for(int i=0;i= 0; k--) {// for(int + // i2=0;i2l_ave +=(255-data[i][i2][0])+(255-data[i][i2][1])+(255-data[i][i2][2]); + data_i_k = data_i[k]; + l_ave += 255 * 3 - data_i_k[0] - data_i_k[1] - data_i_k[2]; + } + } + l_ave /= (lheight * lwidth * 3); + for (i = lheight - 1; i >= 0; i--) {// for(i=0;i= 0; k--) {// for(i2=0;i2 + // for(int i3=0;i3<3;i3++){ + // input[i][i2][i3] = (255-data[i][i2][i3]) - l_ave; + // sum += input[i][i2][i3]*input[i][i2][i3]; + // } + data_i_k = data_i[k]; + input_i_k = input_i[k]; + w_sum = (255 - data_i_k[0]) - l_ave; + input_i_k[0] = w_sum; + sum += w_sum * w_sum; + + w_sum = (255 - data_i_k[1]) - l_ave; + input_i_k[1] = w_sum; + sum += w_sum * w_sum; + + w_sum = (255 - data_i_k[2]) - l_ave; + input_i_k[2] = w_sum; + sum += w_sum * w_sum; + // + } + } + datapow = Math.sqrt((double) sum); + if (datapow == 0.0) { + return false;// throw new NyARException(); + // dir.set(0);//*dir = 0; + // cf.set(-1.0);//*cf = -1.0; + // return -1; + } + return true; + } + + /** + * public int pattern_match(short[][][] data,IntPointer dir,DoublePointer + * cf) + * + */ + public void evaluate(NyARCode i_code) + { + int[][][][] pat = i_code.getPat(); + double[] patpow = i_code.getPatPow(); + int res = -1; + double max = 0.0; + int[][][] pat_j, linput; + int[][] pat_j_i, input_i; + int[] pat_j_i_k, input_i_k; + int l_width = this.width; + int l_height = this.height; + linput = this.input; + for (int j = 0; j < 4; j++) { + int sum = 0; + pat_j = pat[j]; + for (int i = l_height - 1; i >= 0; i--) {// for(int i=0;i= 0; k--) { + pat_j_i_k = pat_j_i[k]; + input_i_k = input_i[k]; + // for(int i3=0;i3<3;i3++){ + sum += input_i_k[0] * pat_j_i_k[0];// sum +=input[i][i2][i3]*pat[k][j][i][i2][i3]; + sum += input_i_k[1] * pat_j_i_k[1];// sum +=input[i][i2][i3]*pat[k][j][i][i2][i3]; + sum += input_i_k[2] * pat_j_i_k[2];// sum +=input[i][i2][i3]*pat[k][j][i][i2][i3]; + // } + } + } + double sum2 = sum / patpow[j] / datapow;// sum2 = sum / patpow[k][j]/ datapow; + if (sum2 > max) { + max = sum2; + res = j; + } + } + dir = res; + cf = max; + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITH_PCA.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITH_PCA.java new file mode 100644 index 0000000..0cfd37c --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITH_PCA.java @@ -0,0 +1,168 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.match; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARCode; +import jp.nyatla.nyartoolkit.core.pickup.INyARColorPatt; + +/** + * AR_TEMPLATE_MATCHING_COLORかつAR_MATCHING_WITH_PCAと同等のルールで マーカーを評価します。 + * + */ +public class NyARMatchPatt_Color_WITH_PCA implements INyARMatchPatt +{ + private final int EVEC_MAX = 10;// #define EVEC_MAX 10 + + private int evec_dim;// static int evec_dim; + + private int[][][] input; + + private double[][][][] evec;// static double evec[EVEC_MAX][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3]; + + private double[][] epat = new double[4][EVEC_MAX];// static double epat[AR_PATT_NUM_MAX][4][EVEC_MAX]; + + private int ave; + + private double datapow; + + private int width; + + private int height; + + private double cf = 0; + + private int dir = 0;// 向きか! + + public double getConfidence() + { + return cf; + } + + public int getDirection() + { + return dir; + } + + public boolean setPatt(INyARColorPatt i_target_patt) throws NyARException + { + width = i_target_patt.getWidth(); + height = i_target_patt.getHeight(); + int[][][] data = i_target_patt.getPatArray(); + + input = new int[height][width][3]; + evec = new double[EVEC_MAX][height][width][3];// static double evec[EVEC_MAX][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3]; + int sum; + + sum = ave = 0; + for (int i = 0; i < height; i++) {// for(int i=0;i + * + */ +package jp.nyatla.nyartoolkit.core.param; + +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; +import jp.nyatla.utils.DoubleValue; + +/** + * カメラの歪み成分を格納するクラスと、補正関数群 + * http://www.hitl.washington.edu/artoolkit/Papers/ART02-Tutorial.pdf + * 11ページを読むといいよ。 + * + * x=x(xi-x0),y=s(yi-y0) + * d^2=x^2+y^2 + * p=(1-fd^2) + * xd=px+x0,yd=py+y0 + */ +final public class NyARCameraDistortionFactor +{ + private static final int PD_LOOP = 3; + private double _f0;//x0 + private double _f1;//y0 + private double _f2;//100000000.0*f + private double _f3;//s + /** + * 配列の値をファクタ値としてセットする。 + * @param i_factor + * 4要素以上の配列 + */ + public void setValue(double[] i_factor) + { + this._f0=i_factor[0]; + this._f1=i_factor[1]; + this._f2=i_factor[2]; + this._f3=i_factor[3]; + return; + } + public void getValue(double[] o_factor) + { + o_factor[0]=this._f0; + o_factor[1]=this._f1; + o_factor[2]=this._f2; + o_factor[3]=this._f3; + return; + } + public void changeScale(double i_scale) + { + this._f0=this._f0*i_scale;// newparam->dist_factor[0] =source->dist_factor[0] *scale; + this._f1=this._f1*i_scale;// newparam->dist_factor[1] =source->dist_factor[1] *scale; + this._f2=this._f2/ (i_scale * i_scale);// newparam->dist_factor[2]=source->dist_factor[2]/ (scale*scale); + //this.f3=this.f3;// newparam->dist_factor[3] =source->dist_factor[3]; + return; + } + /** + * int arParamIdeal2Observ( const double dist_factor[4], const double ix,const double iy,double *ox, double *oy ) 関数の代替関数 + * + * @param i_in + * @param o_out + */ + public void ideal2Observ(final NyARDoublePoint2d i_in, NyARDoublePoint2d o_out) + { + final double x = (i_in.x - this._f0) * this._f3; + final double y = (i_in.y - this._f1) * this._f3; + if (x == 0.0 && y == 0.0) { + o_out.x = this._f0; + o_out.y = this._f1; + } else { + final double d = 1.0 - this._f2 / 100000000.0 * (x * x + y * y); + o_out.x = x * d + this._f0; + o_out.y = y * d + this._f1; + } + return; + } + + /** + * ideal2Observをまとめて実行します。 + * @param i_in + * @param o_out + */ + public void ideal2ObservBatch(final NyARDoublePoint2d[] i_in, NyARDoublePoint2d[] o_out, int i_size) + { + double x, y; + final double d0 = this._f0; + final double d1 = this._f1; + final double d3 = this._f3; + final double d2_w = this._f2 / 100000000.0; + for (int i = 0; i < i_size; i++) { + x = (i_in[i].x - d0) * d3; + y = (i_in[i].y - d1) * d3; + if (x == 0.0 && y == 0.0) { + o_out[i].x = d0; + o_out[i].y = d1; + } else { + final double d = 1.0 - d2_w * (x * x + y * y); + o_out[i].x = x * d + d0; + o_out[i].y = y * d + d1; + } + } + return; + } + + /** + * int arParamObserv2Ideal( const double dist_factor[4], const double ox,const double oy,double *ix, double *iy ); + * + * @param ox + * @param oy + * @param ix + * @param iy + * @return + */ + public void observ2Ideal(double ox, double oy, DoubleValue ix, DoubleValue iy) + { + double z02, z0, p, q, z, px, py, opttmp_1; + final double d0 = this._f0; + final double d1 = this._f1; + + px = ox - d0; + py = oy - d1; + p = this._f2 / 100000000.0; + z02 = px * px + py * py; + q = z0 = Math.sqrt(z02);// Optimize//q = z0 = Math.sqrt(px*px+ py*py); + + for (int i = 1;; i++) { + if (z0 != 0.0) { + // Optimize opttmp_1 + opttmp_1 = p * z02; + z = z0 - ((1.0 - opttmp_1) * z0 - q) / (1.0 - 3.0 * opttmp_1); + px = px * z / z0; + py = py * z / z0; + } else { + px = 0.0; + py = 0.0; + break; + } + if (i == PD_LOOP) { + break; + } + z02 = px * px + py * py; + z0 = Math.sqrt(z02);// Optimize//z0 = Math.sqrt(px*px+ py*py); + } + ix.value = px / this._f3 + d0; + iy.value = py / this._f3 + d1; + return; + } + + /** + * 指定範囲のobserv2Idealをまとめて実行して、結果をo_idealに格納します。 + * + * @param i_x_coord + * @param i_y_coord + * @param i_start + * coord開始点 + * @param i_num + * 計算数 + * @param o_ideal + * 出力バッファ[i_num][2]であること。 + */ + public void observ2IdealBatch(int[] i_x_coord, int[] i_y_coord,int i_start, int i_num, double[][] o_ideal) + { + double z02, z0, q, z, px, py, opttmp_1; + final double d0 = this._f0; + final double d1 = this._f1; + final double d3 = this._f3; + final double p = this._f2 / 100000000.0; + for (int j = 0; j < i_num; j++) { + + px = i_x_coord[i_start + j] - d0; + py = i_y_coord[i_start + j] - d1; + + z02 = px * px + py * py; + q = z0 = Math.sqrt(z02);// Optimize//q = z0 = Math.sqrt(px*px+py*py); + + for (int i = 1;; i++) { + if (z0 != 0.0) { + // Optimize opttmp_1 + opttmp_1 = p * z02; + z = z0 - ((1.0 - opttmp_1) * z0 - q)/ (1.0 - 3.0 * opttmp_1); + px = px * z / z0; + py = py * z / z0; + } else { + px = 0.0; + py = 0.0; + break; + } + if (i == PD_LOOP) { + break; + } + z02 = px * px + py * py; + z0 = Math.sqrt(z02);// Optimize//z0 = Math.sqrt(px*px+ py*py); + } + o_ideal[j][0] = px / d3 + d0; + o_ideal[j][1] = py / d3 + d1; + } + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/param/NyARParam.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/param/NyARParam.java new file mode 100644 index 0000000..4267207 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/param/NyARParam.java @@ -0,0 +1,173 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.param; + +import java.io.*; +import java.nio.*; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * typedef struct { int xsize, ysize; double mat[3][4]; double dist_factor[4]; } ARParam; + * NyARの動作パラメータを格納するクラス + * + */ +public class NyARParam +{ + protected NyARIntSize _screen_size=new NyARIntSize(); + private static final int SIZE_OF_PARAM_SET = 4 + 4 + (3 * 4 * 8) + (4 * 8); + private NyARCameraDistortionFactor _dist=new NyARCameraDistortionFactor(); + private NyARPerspectiveProjectionMatrix _projection_matrix=new NyARPerspectiveProjectionMatrix(); + + public NyARIntSize getScreenSize() + { + return this._screen_size; + } + + public NyARPerspectiveProjectionMatrix getPerspectiveProjectionMatrix() + { + return this._projection_matrix; + } + public NyARCameraDistortionFactor getDistortionFactor() + { + return this._dist; + } + + /** + * ARToolKit標準ファイルから1個目の設定をロードする。 + * + * @param i_filename + * @throws NyARException + */ + public void loadARParamFromFile(String i_filename) throws NyARException + { + try { + loadARParam(new FileInputStream(i_filename)); + } catch (Exception e) { + throw new NyARException(e); + } + } + + /** + * int arParamChangeSize( ARParam *source, int xsize, int ysize, ARParam *newparam ); + * 関数の代替関数 サイズプロパティをi_xsize,i_ysizeに変更します。 + * @param i_xsize + * @param i_ysize + * @param newparam + * @return + * + */ + public void changeScreenSize(int i_xsize, int i_ysize) + { + final double scale = (double) i_xsize / (double) (this._screen_size.w);// scale = (double)xsize / (double)(source->xsize); + //スケールを変更 + this._dist.changeScale(scale); + this._projection_matrix.changeScale(scale); + //for (int i = 0; i < 4; i++) { + // array34[0 * 4 + i] = array34[0 * 4 + i] * scale;// newparam->mat[0][i]=source->mat[0][i]* scale; + // array34[1 * 4 + i] = array34[1 * 4 + i] * scale;// newparam->mat[1][i]=source->mat[1][i]* scale; + // array34[2 * 4 + i] = array34[2 * 4 + i];// newparam->mat[2][i] = source->mat[2][i]; + //} + + + this._screen_size.w = i_xsize;// newparam->xsize = xsize; + this._screen_size.h = i_ysize;// newparam->ysize = ysize; + return; + } + + + /** + * int arParamLoad( const char *filename, int num, ARParam *param, ...); + * i_streamの入力ストリームからi_num個の設定を読み込み、パラメタを配列にして返します。 + * + * @param i_stream + * @throws Exception + */ + public void loadARParam(InputStream i_stream)throws NyARException + { + try { + byte[] buf = new byte[SIZE_OF_PARAM_SET]; + i_stream.read(buf); + double[] tmp=new double[12]; + + // バッファを加工 + ByteBuffer bb = ByteBuffer.wrap(buf); + bb.order(ByteOrder.BIG_ENDIAN); + this._screen_size.w = bb.getInt(); + this._screen_size.h = bb.getInt(); + //double値を12個読み込む + for(int i=0;i<12;i++){ + tmp[i]=bb.getDouble(); + } + //Projectionオブジェクトにセット + this._projection_matrix.setValue(tmp); + //double値を4個読み込む + for (int i = 0; i < 4; i++) { + tmp[i]=bb.getDouble(); + } + //Factorオブジェクトにセット + this._dist.setValue(tmp); + } catch (Exception e) { + throw new NyARException(e); + } + return; + } + + public void saveARParam(OutputStream i_stream)throws Exception + { + NyARException.trap("未チェックの関数"); + byte[] buf = new byte[SIZE_OF_PARAM_SET]; + // バッファをラップ + ByteBuffer bb = ByteBuffer.wrap(buf); + bb.order(ByteOrder.BIG_ENDIAN); + + // 書き込み + bb.putInt(this._screen_size.w); + bb.putInt(this._screen_size.h); + double[] tmp=new double[12]; + //Projectionを読み出し + this._projection_matrix.getValue(tmp); + //double値を12個書き込む + for(int i=0;i<12;i++){ + tmp[i]=bb.getDouble(); + } + //Factorを読み出し + this._dist.getValue(tmp); + //double値を4個書き込む + for (int i = 0; i < 4; i++) { + tmp[i]=bb.getDouble(); + } + i_stream.write(buf); + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/param/NyARPerspectiveProjectionMatrix.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/param/NyARPerspectiveProjectionMatrix.java new file mode 100644 index 0000000..c025160 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/param/NyARPerspectiveProjectionMatrix.java @@ -0,0 +1,181 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.param; + +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix34; + +/** + * 透視変換行列を格納します。 + * http://www.hitl.washington.edu/artoolkit/Papers/ART02-Tutorial.pdf + * 7ページを見るといいよ。 + * + */ +final public class NyARPerspectiveProjectionMatrix extends NyARDoubleMatrix34 +{ + /* + * static double dot( double a1, double a2, double a3,double b1, double b2,double b3 ) + */ + private final static double dot(double a1, double a2, double a3, double b1,double b2, double b3) + { + return (a1 * b1 + a2 * b2 + a3 * b3); + } + + /* static double norm( double a, double b, double c ) */ + private final static double norm(double a, double b, double c) + { + return Math.sqrt(a * a + b * b + c * c); + } + + /** + * int arParamDecompMat( double source[3][4], double cpara[3][4], double trans[3][4] ); 関数の置き換え Optimize STEP[754->665] + * + * @param o_cpara + * 戻り引数。3x4のマトリクスを指定すること。 + * @param o_trans + * 戻り引数。3x4のマトリクスを指定すること。 + * @return + */ + public void decompMat(NyARMat o_cpara, NyARMat o_trans) + { + double rem1, rem2, rem3; + double c00,c01,c02,c03,c10,c11,c12,c13,c20,c21,c22,c23; + if (this.m23>= 0) {// if( source[2][3] >= 0 ) { + // + // for(int r = 0; r < 3; r++ ){ + // for(int c = 0; c < 4; c++ ){ + // Cpara[r][c]=source[r][c];//Cpara[r][c] = source[r][c]; + // } + // } + c00=this.m00; + c01=this.m01; + c02=this.m02; + c03=this.m03; + c10=this.m10; + c11=this.m11; + c12=this.m12; + c13=this.m13; + c20=this.m20; + c21=this.m21; + c22=this.m22; + c23=this.m23; + } else { + // + // for(int r = 0; r < 3; r++ ){ + // for(int c = 0; c < 4; c++ ){ + // Cpara[r][c]=-source[r][c];//Cpara[r][c] = -(source[r][c]); + // } + // } + c00=-this.m00; + c01=-this.m01; + c02=-this.m02; + c03=-this.m03; + c10=-this.m10; + c11=-this.m11; + c12=-this.m12; + c13=-this.m13; + c20=-this.m20; + c21=-this.m21; + c22=-this.m22; + c23=-this.m23; + } + + double[][] cpara = o_cpara.getArray(); + double[][] trans = o_trans.getArray(); + for (int r = 0; r < 3; r++) { + for (int c = 0; c < 4; c++) { + cpara[r][c] = 0.0;// cpara[r][c] = 0.0; + } + } + cpara[2][2] = norm(c20, c21, c22);// cpara[2][2] =norm( Cpara[2][0],Cpara[2][1],Cpara[2][2]); + trans[2][0] = c20 / cpara[2][2];// trans[2][0] = Cpara[2][0] /cpara[2][2]; + trans[2][1] = c21 / cpara[2][2];// trans[2][1] = Cpara[2][1] / cpara[2][2]; + trans[2][2] = c22 / cpara[2][2];// trans[2][2] =Cpara[2][2] /cpara[2][2]; + trans[2][3] = c23 / cpara[2][2];// trans[2][3] =Cpara[2][3] /cpara[2][2]; + + cpara[1][2] = dot(trans[2][0], trans[2][1], trans[2][2], c10, c11, c12);// cpara[1][2]=dot(trans[2][0],trans[2][1],trans[2][2],Cpara[1][0],Cpara[1][1],Cpara[1][2]); + rem1 = c10 - cpara[1][2] * trans[2][0];// rem1 =Cpara[1][0] -cpara[1][2] *trans[2][0]; + rem2 = c11 - cpara[1][2] * trans[2][1];// rem2 =Cpara[1][1] -cpara[1][2] *trans[2][1]; + rem3 = c12 - cpara[1][2] * trans[2][2];// rem3 =Cpara[1][2] -cpara[1][2] *trans[2][2]; + cpara[1][1] = norm(rem1, rem2, rem3);// cpara[1][1] = norm( rem1,// rem2, rem3 ); + trans[1][0] = rem1 / cpara[1][1];// trans[1][0] = rem1 / cpara[1][1]; + trans[1][1] = rem2 / cpara[1][1];// trans[1][1] = rem2 / cpara[1][1]; + trans[1][2] = rem3 / cpara[1][1];// trans[1][2] = rem3 / cpara[1][1]; + + cpara[0][2] = dot(trans[2][0], trans[2][1], trans[2][2], c00, c01, c02);// cpara[0][2] =dot(trans[2][0], trans[2][1],trans[2][2],Cpara[0][0],Cpara[0][1],Cpara[0][2]); + cpara[0][1] = dot(trans[1][0], trans[1][1], trans[1][2], c00, c01, c02);// cpara[0][1]=dot(trans[1][0],trans[1][1],trans[1][2],Cpara[0][0],Cpara[0][1],Cpara[0][2]); + rem1 = c00 - cpara[0][1] * trans[1][0] - cpara[0][2]* trans[2][0];// rem1 = Cpara[0][0] - cpara[0][1]*trans[1][0]- cpara[0][2]*trans[2][0]; + rem2 = c01 - cpara[0][1] * trans[1][1] - cpara[0][2]* trans[2][1];// rem2 = Cpara[0][1] - cpara[0][1]*trans[1][1]- cpara[0][2]*trans[2][1]; + rem3 = c02 - cpara[0][1] * trans[1][2] - cpara[0][2]* trans[2][2];// rem3 = Cpara[0][2] - cpara[0][1]*trans[1][2] - cpara[0][2]*trans[2][2]; + cpara[0][0] = norm(rem1, rem2, rem3);// cpara[0][0] = norm( rem1,rem2, rem3 ); + trans[0][0] = rem1 / cpara[0][0];// trans[0][0] = rem1 / cpara[0][0]; + trans[0][1] = rem2 / cpara[0][0];// trans[0][1] = rem2 / cpara[0][0]; + trans[0][2] = rem3 / cpara[0][0];// trans[0][2] = rem3 / cpara[0][0]; + + trans[1][3] = (c13 - cpara[1][2] * trans[2][3])/ cpara[1][1];// trans[1][3] = (Cpara[1][3] -cpara[1][2]*trans[2][3]) / cpara[1][1]; + trans[0][3] = (c03 - cpara[0][1] * trans[1][3] - cpara[0][2]* trans[2][3])/ cpara[0][0];// trans[0][3] = (Cpara[0][3] -cpara[0][1]*trans[1][3]-cpara[0][2]*trans[2][3]) / cpara[0][0]; + + for (int r = 0; r < 3; r++) { + for (int c = 0; c < 3; c++) { + cpara[r][c] /= cpara[2][2];// cpara[r][c] /= cpara[2][2]; + } + } + return; + } + /** + * int arParamChangeSize( ARParam *source, int xsize, int ysize, ARParam *newparam ); + * Matrixのスケールを変換します。 + * @param i_scale + * + */ + public void changeScale(double i_scale) + { + this.m00=this.m00*i_scale; + this.m10=this.m10*i_scale; + this.m01=this.m01*i_scale; + this.m11=this.m11*i_scale; + this.m02=this.m02*i_scale; + this.m12=this.m12*i_scale; + this.m03=this.m03*i_scale; + this.m13=this.m13*i_scale; + //for (int i = 0; i < 4; i++) { + // array34[0 * 4 + i] = array34[0 * 4 + i] * scale;// newparam->mat[0][i]=source->mat[0][i]* scale; + // array34[1 * 4 + i] = array34[1 * 4 + i] * scale;// newparam->mat[1][i]=source->mat[1][i]* scale; + // array34[2 * 4 + i] = array34[2 * 4 + i];// newparam->mat[2][i] = source->mat[2][i]; + //} + return; + } + + + + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/INyARColorPatt.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/INyARColorPatt.java new file mode 100644 index 0000000..a56479c --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/INyARColorPatt.java @@ -0,0 +1,71 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.pickup; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARSquare; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; + +public interface INyARColorPatt +{ + /** + * カラーパターンの幅をピクセル値で返します。 + * + * @return + */ + public int getWidth(); + + /** + * カラーパターンの高さをピクセル値で返します。 + * + * @return + */ + public int getHeight(); + /** + * カメラパターンを格納した配列への参照値を返します。 配列は最低でも[height][wight][3]のサイズを持ちますが、 + * 配列のlengthとwidth,heightの数は一致しないことがあります。 + * setSize関数を実行すると、以前に呼び出されたgetPatArrayが返した値は不定になります。 + * + * @return + */ + public int[][][] getPatArray(); + + /** + * ラスタイメージからi_square部分のカラーパターンを抽出して、保持します。 + * + * @param image + * @param i_square + * @return ラスターの取得に成功するとTRUE/失敗するとFALSE + * @throws NyARException + */ + public boolean pickFromRaster(INyARRgbRaster image, NyARSquare i_square) throws NyARException; +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_O1.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_O1.java new file mode 100644 index 0000000..a8edbbf --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_O1.java @@ -0,0 +1,339 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.pickup; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARMat; +import jp.nyatla.nyartoolkit.core.NyARSquare; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; +import jp.nyatla.nyartoolkit.core.rasterreader.*; +/** + * 24ビットカラーのマーカーを保持するために使うクラスです。 このクラスは、ARToolkitのパターンと、ラスタから取得したパターンを保持します。 + * 演算順序以外の最適化をしたもの + * + */ +public class NyARColorPatt_O1 implements INyARColorPatt +{ + private static final int AR_PATT_SAMPLE_NUM = 64;// #define + // AR_PATT_SAMPLE_NUM 64 + + private int extpat[][][]; + + private int width; + + private int height; + + public NyARColorPatt_O1(int i_width, int i_height) + { + this.width = i_width; + this.height = i_height; + this.extpat = new int[i_height][i_width][3]; + this.wk_pickFromRaster_ext_pat2 = new int[i_height][i_width][3]; + } + + // public void setSize(int i_new_width,int i_new_height) + // { + // int array_w=this.extpat[0].length; + // int array_h=this.extpat.length; + // //十分なサイズのバッファがあるか確認 + // if(array_w>=i_new_width && array_h>=i_new_height){ + // //OK 十分だ→サイズ調整のみ + // }else{ + // //足りないよ→取り直し + // this.wk_pickFromRaster_ext_pat2=new int[i_new_height][i_new_width][3]; + // this.extpat=new int[i_new_height][i_new_width][3]; + // } + // this.width =i_new_width; + // this.height=i_new_height; + // return; + // } + + public int[][][] getPatArray() + { + return extpat; + } + + public int getWidth() + { + return width; + } + + public int getHeight() + { + return height; + } + + private final NyARMat wk_get_cpara_a = new NyARMat(8, 8); + + private final NyARMat wk_get_cpara_b = new NyARMat(8, 1); + + private final NyARMat wk_get_cpara_c = new NyARMat(8, 1); + + /** + * + * @param world + * @param vertex + * @param para + * [3x3] + * @throws NyARException + */ + private boolean get_cpara(double world[][], double vertex[][], double[] para) + throws NyARException + { + NyARMat a = wk_get_cpara_a;// 次処理で値を設定するので、初期化不要// new NyARMat( 8, 8 ); + double[][] a_array = a.getArray(); + NyARMat b = wk_get_cpara_b;// 次処理で値を設定するので、初期化不要// new NyARMat( 8, 1 ); + double[][] b_array = b.getArray(); + double[] a_pt0, a_pt1, world_pti; + + for (int i = 0; i < 4; i++) { + a_pt0 = a_array[i * 2]; + a_pt1 = a_array[i * 2 + 1]; + world_pti = world[i]; + + a_pt0[0] = world_pti[0];// a->m[i*16+0] = world[i][0]; + a_pt0[1] = world_pti[1];// a->m[i*16+1] = world[i][1]; + a_pt0[2] = 1.0;// a->m[i*16+2] = 1.0; + a_pt0[3] = 0.0;// a->m[i*16+3] = 0.0; + a_pt0[4] = 0.0;// a->m[i*16+4] = 0.0; + a_pt0[5] = 0.0;// a->m[i*16+5] = 0.0; + a_pt0[6] = -world_pti[0] * vertex[i][0];// a->m[i*16+6] = + // -world[i][0] * + // vertex[i][0]; + a_pt0[7] = -world_pti[1] * vertex[i][0];// a->m[i*16+7] = + // -world[i][1] * + // vertex[i][0]; + a_pt1[0] = 0.0;// a->m[i*16+8] = 0.0; + a_pt1[1] = 0.0;// a->m[i*16+9] = 0.0; + a_pt1[2] = 0.0;// a->m[i*16+10] = 0.0; + a_pt1[3] = world_pti[0];// a->m[i*16+11] = world[i][0]; + a_pt1[4] = world_pti[1];// a->m[i*16+12] = world[i][1]; + a_pt1[5] = 1.0;// a->m[i*16+13] = 1.0; + a_pt1[6] = -world_pti[0] * vertex[i][1];// a->m[i*16+14] = + // -world[i][0] * + // vertex[i][1]; + a_pt1[7] = -world_pti[1] * vertex[i][1];// a->m[i*16+15] = + // -world[i][1] * + // vertex[i][1]; + b_array[i * 2 + 0][0] = vertex[i][0];// b->m[i*2+0] = + // vertex[i][0]; + b_array[i * 2 + 1][0] = vertex[i][1];// b->m[i*2+1] = + // vertex[i][1]; + } + // JartkException.trap("未チェックのパス"); + if (!a.matrixSelfInv()) { + return false;// 逆行列を求められないので失敗 + } + + // JartkException.trap("未チェックのパス"); + NyARMat c = wk_get_cpara_c;// 次処理で結果を受け取るので、初期化不要//new NyARMat( 8, 1 ); + double[][] c_array = c.getArray(); + + c.matrixMul(a, b); + for (int i = 0; i < 2; i++) { + para[i * 3 + 0] = c_array[i * 3 + 0][0];// para[i][0] = c->m[i*3+0]; + para[i * 3 + 1] = c_array[i * 3 + 1][0];// para[i][1] = c->m[i*3+1]; + para[i * 3 + 2] = c_array[i * 3 + 2][0];// para[i][2] = c->m[i*3+2]; + } + para[2 * 3 + 0] = c_array[2 * 3 + 0][0];// para[2][0] = c->m[2*3+0]; + para[2 * 3 + 1] = c_array[2 * 3 + 1][0];// para[2][1] = c->m[2*3+1]; + para[2 * 3 + 2] = 1.0;// para[2][2] = 1.0; + return true; + } + + private final double[][] wk_pickFromRaster_local = new double[4][2]; + + private final double[] wk_pickFromRaster_para = new double[9];// [3][3]; + + private int[][][] wk_pickFromRaster_ext_pat2 = null;// コンストラクタでint[height][width][3]を作る + + private final double[][] wk_pickFromRaster_world = {// double world[4][2]; + { 100.0, 100.0 }, { 100.0 + 10.0, 100.0 }, { 100.0 + 10.0, 100.0 + 10.0 }, + { 100.0, 100.0 + 10.0 } }; + + /** + * pickFromRaster関数から使う変数です。 + * + */ + private static void initValue_wk_pickFromRaster_ext_pat2( + int[][][] i_ext_pat2, int i_width, int i_height) + { + int i, i2; + int[][] pt2; + int[] pt1; + for (i = i_height - 1; i >= 0; i--) { + pt2 = i_ext_pat2[i]; + for (i2 = i_width - 1; i2 >= 0; i2--) { + pt1 = pt2[i2]; + pt1[0] = 0; + pt1[1] = 0; + pt1[2] = 0; + } + } + } + + private final int[] wk_pickFromRaster_rgb_tmp = new int[3]; + + /** + * imageから、i_markerの位置にあるパターンを切り出して、保持します。 Optimize:STEP[769->] + * + * @param image + * @param i_marker + * @return 切り出しに失敗した + * @throws Exception + */ + public boolean pickFromRaster(INyARRgbRaster image, NyARSquare i_square)throws NyARException + { + double d, xw, yw; + int xc, yc; + int xdiv, ydiv; + int xdiv2, ydiv2; + int lx1, lx2, ly1, ly2; + + int img_x = image.getWidth(); + int img_y = image.getHeight(); + + double xdiv2_reciprocal; // [tp] + double ydiv2_reciprocal; // [tp] + + // int[] x_coord=i_marker.x_coord; + // int[] y_coord=i_marker.y_coord; + // int[] vertex=i_marker.mkvertex; + double[][] local = wk_pickFromRaster_local;// double local[4][2]; + // + for (int i = 0; i < 4; i++) { + local[i][0] = i_square.imvertex[i].x; + local[i][1] = i_square.imvertex[i].y; + } + + double[][] world = wk_pickFromRaster_world; + /* + * world[0][0] = 100.0; world[0][1] = 100.0; world[1][0] = 100.0 + 10.0; + * world[1][1] = 100.0; world[2][0] = 100.0 + 10.0; world[2][1] = 100.0 + + * 10.0; world[3][0] = 100.0; world[3][1] = 100.0 + 10.0; + */ + double[] para = wk_pickFromRaster_para; // double para[3][3]; + // パターンの切り出しに失敗することもある。 + if (!get_cpara(world, local, para)) { + return false; + } + lx1 = (int) ((local[0][0] - local[1][0]) * (local[0][0] - local[1][0]) + (local[0][1] - local[1][1]) + * (local[0][1] - local[1][1])); + lx2 = (int) ((local[2][0] - local[3][0]) * (local[2][0] - local[3][0]) + (local[2][1] - local[3][1]) + * (local[2][1] - local[3][1])); + ly1 = (int) ((local[1][0] - local[2][0]) * (local[1][0] - local[2][0]) + (local[1][1] - local[2][1]) + * (local[1][1] - local[2][1])); + ly2 = (int) ((local[3][0] - local[0][0]) * (local[3][0] - local[0][0]) + (local[3][1] - local[0][1]) + * (local[3][1] - local[0][1])); + if (lx2 > lx1) { + lx1 = lx2; + } + if (ly2 > ly1) { + ly1 = ly2; + } + xdiv2 = this.width; + ydiv2 = this.height; + + while (xdiv2 * xdiv2 < lx1 / 4) { + xdiv2 *= 2; + } + while (ydiv2 * ydiv2 < ly1 / 4) { + ydiv2 *= 2; + } + + if (xdiv2 > AR_PATT_SAMPLE_NUM) { + xdiv2 = AR_PATT_SAMPLE_NUM; + } + if (ydiv2 > AR_PATT_SAMPLE_NUM) { + ydiv2 = AR_PATT_SAMPLE_NUM; + } + + xdiv = xdiv2 / width;// xdiv = xdiv2/Config.AR_PATT_SIZE_X; + ydiv = ydiv2 / height;// ydiv = ydiv2/Config.AR_PATT_SIZE_Y; + + /* wk_pickFromRaster_ext_pat2ワーク変数を初期化する。 */ + int[][][] ext_pat2 = wk_pickFromRaster_ext_pat2;// ARUint32 ext_pat2[AR_PATT_SIZE_Y][AR_PATT_SIZE_X][3]; + int extpat_j[][], extpat_j_i[]; + int ext_pat2_j[][], ext_pat2_j_i[]; + + initValue_wk_pickFromRaster_ext_pat2(ext_pat2, this.width, this.height); + + xdiv2_reciprocal = 1.0 / xdiv2; + ydiv2_reciprocal = 1.0 / ydiv2; + int i, j; + int[] rgb_tmp = wk_pickFromRaster_rgb_tmp; + + //ピクセルリーダーを取得 + INyARRgbPixelReader reader=image.getRgbPixelReader(); + + // arGetCode_put_zero(ext_pat2);//put_zero( (ARUint8 *)ext_pat2, + // AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3*sizeof(ARUint32) ); + for (j = 0; j < ydiv2; j++) { + yw = 102.5 + 5.0 * (j + 0.5) * ydiv2_reciprocal; + for (i = 0; i < xdiv2; i++) { + xw = 102.5 + 5.0 * (i + 0.5) * xdiv2_reciprocal; + d = para[2 * 3 + 0] * xw + para[2 * 3 + 1] * yw+ para[2 * 3 + 2]; + if (d == 0) { + throw new NyARException(); + } + xc = (int) ((para[0 * 3 + 0] * xw + para[0 * 3 + 1] * yw + para[0 * 3 + 2]) / d); + yc = (int) ((para[1 * 3 + 0] * xw + para[1 * 3 + 1] * yw + para[1 * 3 + 2]) / d); + + if (xc >= 0 && xc < img_x && yc >= 0 && yc < img_y) { + reader.getPixel(xc, yc, rgb_tmp); + ext_pat2_j_i = ext_pat2[j / ydiv][i / xdiv]; + + ext_pat2_j_i[0] += rgb_tmp[0];// R + ext_pat2_j_i[1] += rgb_tmp[1];// G + ext_pat2_j_i[2] += rgb_tmp[2];// B + // System.out.println(xc+":"+yc+":"+rgb_tmp[0]+":"+rgb_tmp[1]+":"+rgb_tmp[2]); + } + } + } + // short[][][] ext_pat=new short[Config.AR_PATT_SIZE_Y][Config.AR_PATT_SIZE_X][3];//ARUint32 + // ext_pat2[AR_PATT_SIZE_Y][AR_PATT_SIZE_X][3]; + /* */ + int xdiv_x_ydiv = xdiv * ydiv; + for (j = this.height - 1; j >= 0; j--) { + extpat_j = extpat[j]; + ext_pat2_j = ext_pat2[j]; + for (i = this.width - 1; i >= 0; i--) { // PRL 2006-06-08. + ext_pat2_j_i = ext_pat2_j[i]; + extpat_j_i = extpat_j[i]; + extpat_j_i[0] = (ext_pat2_j_i[0] / xdiv_x_ydiv);// ext_pat[j][i][0]=(byte)(ext_pat2[j][i][0] / (xdiv*ydiv)); + extpat_j_i[1] = (ext_pat2_j_i[1] / xdiv_x_ydiv);// ext_pat[j][i][1]=(byte)(ext_pat2[j][i][1]/(xdiv*ydiv)); + extpat_j_i[2] = (ext_pat2_j_i[2] / xdiv_x_ydiv);// ext_pat[j][i][2]=(byte)(ext_pat2[j][i][2]/(xdiv*ydiv)); + } + } + return true; + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_O3.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_O3.java new file mode 100644 index 0000000..27711a1 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_O3.java @@ -0,0 +1,391 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.pickup; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARMat; +import jp.nyatla.nyartoolkit.core.NyARSquare; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; +import jp.nyatla.nyartoolkit.core.rasterreader.*; +import jp.nyatla.nyartoolkit.core.types.*; +/** + * 24ビットカラーのマーカーを保持するために使うクラスです。 このクラスは、ARToolkitのパターンと、ラスタから取得したパターンを保持します。 + * 演算順序を含む最適化をしたもの + * + */ +public class NyARColorPatt_O3 implements INyARColorPatt +{ + private static final int AR_PATT_SAMPLE_NUM = 64;// #define + // AR_PATT_SAMPLE_NUM 64 + + private int[][][] extpat; + + private int width; + + private int height; + + public NyARColorPatt_O3(int i_width, int i_height) + { + this.width = i_width; + this.height = i_height; + this.extpat = new int[i_height][i_width][3]; + } + + // public void setSize(int i_new_width,int i_new_height) + // { + // int array_w=this.extpat[0].length; + // int array_h=this.extpat.length; + // //十分なサイズのバッファがあるか確認 + // if(array_w>=i_new_width && array_h>=i_new_height){ + // //OK 十分だ→サイズ調整のみ + // }else{ + // //足りないよ→取り直し + // this.extpat=new int[i_new_height][i_new_width][3]; + // } + // this.width =i_new_width; + // this.height=i_new_height; + // return; + // } + public int[][][] getPatArray() + { + return extpat; + } + + public int getWidth() + { + return width; + } + + public int getHeight() + { + return height; + } + + private final NyARMat wk_get_cpara_a = new NyARMat(8, 8); + + private final NyARMat wk_get_cpara_b = new NyARMat(8, 1); + + /** + * @param world + * @param vertex + * @param o_para + * @throws NyARException + */ + private boolean get_cpara(final NyARIntPoint[] i_vertex, NyARMat o_para)throws NyARException + { + int[][] world = this.wk_pickFromRaster_world; + NyARMat a = wk_get_cpara_a;// 次処理で値を設定するので、初期化不要// new NyARMat( 8, 8 ); + double[][] a_array = a.getArray(); + NyARMat b = wk_get_cpara_b;// 次処理で値を設定するので、初期化不要// new NyARMat( 8, 1 ); + double[][] b_array = b.getArray(); + double[] a_pt0, a_pt1; + int[] world_pti; + + for (int i = 0; i < 4; i++) { + a_pt0 = a_array[i * 2]; + a_pt1 = a_array[i * 2 + 1]; + world_pti = world[i]; + + a_pt0[0] = (double) world_pti[0];// a->m[i*16+0] = world[i][0]; + a_pt0[1] = (double) world_pti[1];// a->m[i*16+1] = world[i][1]; + a_pt0[2] = 1.0;// a->m[i*16+2] = 1.0; + a_pt0[3] = 0.0;// a->m[i*16+3] = 0.0; + a_pt0[4] = 0.0;// a->m[i*16+4] = 0.0; + a_pt0[5] = 0.0;// a->m[i*16+5] = 0.0; + a_pt0[6] = (double) (-world_pti[0] * i_vertex[i].x);// a->m[i*16+6]= -world[i][0]*vertex[i][0]; + a_pt0[7] = (double) (-world_pti[1] * i_vertex[i].x);// a->m[i*16+7]=-world[i][1]*vertex[i][0]; + a_pt1[0] = 0.0;// a->m[i*16+8] = 0.0; + a_pt1[1] = 0.0;// a->m[i*16+9] = 0.0; + a_pt1[2] = 0.0;// a->m[i*16+10] = 0.0; + a_pt1[3] = (double) world_pti[0];// a->m[i*16+11] = world[i][0]; + a_pt1[4] = (double) world_pti[1];// a->m[i*16+12] = world[i][1]; + a_pt1[5] = 1.0;// a->m[i*16+13] = 1.0; + a_pt1[6] = (double) (-world_pti[0] * i_vertex[i].y);// a->m[i*16+14]=-world[i][0]*vertex[i][1]; + a_pt1[7] = (double) (-world_pti[1] * i_vertex[i].y);// a->m[i*16+15]=-world[i][1]*vertex[i][1]; + b_array[i * 2 + 0][0] = (double) i_vertex[i].x;// b->m[i*2+0] =vertex[i][0]; + b_array[i * 2 + 1][0] = (double) i_vertex[i].y;// b->m[i*2+1] =vertex[i][1]; + } + if (!a.matrixSelfInv()) { + return false; + } + + o_para.matrixMul(a, b); + return true; + } + + // private final double[] wk_pickFromRaster_para=new double[9];//[3][3]; + private final int[][] wk_pickFromRaster_world = {// double world[4][2]; + { 100, 100 }, { 100 + 10, 100 }, { 100 + 10, 100 + 10 }, { 100, 100 + 10 } }; + + /** + * pickFromRaster関数から使う変数です。 + * + */ + private static void initValue_wk_pickFromRaster_ext_pat2(int[][][] i_ext_pat2, int i_width, int i_height) + { + int i, i2; + int[][] pt2; + int[] pt1; + for (i = i_height - 1; i >= 0; i--) { + pt2 = i_ext_pat2[i]; + for (i2 = i_width - 1; i2 >= 0; i2--) { + pt1 = pt2[i2]; + pt1[0] = 0; + pt1[1] = 0; + pt1[2] = 0; + } + } + } + + private final NyARMat wk_pickFromRaster_cpara = new NyARMat(8, 1); + + /** + * imageから、i_markerの位置にあるパターンを切り出して、保持します。 Optimize:STEP[769->750] + * + * @param image + * @param i_marker + * @throws Exception + */ + public boolean pickFromRaster(INyARRgbRaster image, NyARSquare i_square)throws NyARException + { + NyARMat cpara = this.wk_pickFromRaster_cpara; + NyARIntPoint[] local = i_square.imvertex; + // //localの計算 + // int[] local_0=wk_pickFromRaster_local[0];//double local[4][2]; + // int[] local_1=wk_pickFromRaster_local[1];//double local[4][2]; + // // + // for(int i = 0; i < 4; i++ ) { + // local_0[i] = i_square.imvertex[i][0]; + // local_1[i] = i_square.imvertex[i][1]; + // } + // xdiv2,ydiv2の計算 + int xdiv2, ydiv2; + int l1, l2; + int w1, w2; + + // x計算 + w1 = local[0].x - local[1].x; + w2 = local[0].y - local[1].y; + l1 = (w1 * w1 + w2 * w2); + w1 = local[2].x - local[3].x; + w2 = local[2].y - local[3].y; + l2 = (w1 * w1 + w2 * w2); + if (l2 > l1) { + l1 = l2; + } + l1 = l1 / 4; + xdiv2 = this.width; + while (xdiv2 * xdiv2 < l1) { + xdiv2 *= 2; + } + if (xdiv2 > AR_PATT_SAMPLE_NUM) { + xdiv2 = AR_PATT_SAMPLE_NUM; + } + + // y計算 + w1 = local[1].x - local[2].x; + w2 = local[1].y - local[2].y; + l1 = (w1 * w1 + w2 * w2); + w1 = local[3].x - local[0].x; + w2 = local[3].y - local[0].y; + l2 = (w1 * w1 + w2 * w2); + if (l2 > l1) { + l1 = l2; + } + ydiv2 = this.height; + l1 = l1 / 4; + while (ydiv2 * ydiv2 < l1) { + ydiv2 *= 2; + } + if (ydiv2 > AR_PATT_SAMPLE_NUM) { + ydiv2 = AR_PATT_SAMPLE_NUM; + } + + // cparaの計算 + if (!get_cpara(local, cpara)) { + return false; + } + updateExtpat(image, cpara, xdiv2, ydiv2); + + return true; + } + + // かなり大きいワークバッファを取るな…。 + private double[] wk_updateExtpat_para00_xw; + + private double[] wk_updateExtpat_para10_xw; + + private double[] wk_updateExtpat_para20_xw; + + private int[] wk_updateExtpat_rgb_buf; + + private int[] wk_updateExtpat_x_rgb_index; + + private int[] wk_updateExtpat_y_rgb_index; + + private int[] wk_updateExtpat_i_rgb_index; + + private int wk_updateExtpat_buffer_size = 0; + + /** + * ワークバッファを予約する + * + * @param i_xdiv2 + */ + private void reservWorkBuffers(int i_xdiv2) + { + if (this.wk_updateExtpat_buffer_size < i_xdiv2) { + wk_updateExtpat_para00_xw = new double[i_xdiv2]; + wk_updateExtpat_para10_xw = new double[i_xdiv2]; + wk_updateExtpat_para20_xw = new double[i_xdiv2]; + wk_updateExtpat_rgb_buf = new int[i_xdiv2 * 3]; + wk_updateExtpat_x_rgb_index = new int[i_xdiv2]; + wk_updateExtpat_y_rgb_index = new int[i_xdiv2]; + wk_updateExtpat_i_rgb_index = new int[i_xdiv2]; + this.wk_updateExtpat_buffer_size = i_xdiv2; + } + // 十分なら何もしない。 + return; + } + + private void updateExtpat(INyARRgbRaster image, NyARMat i_cpara, int i_xdiv2,int i_ydiv2) throws NyARException + { + int img_x = image.getWidth(); + int img_y = image.getHeight(); + final int[][][] L_extpat = this.extpat; + final int L_WIDTH = this.width; + final int L_HEIGHT = this.height; + /* wk_pickFromRaster_ext_pat2ワーク変数を初期化する。 */ + // int[][][] ext_pat2=wk_pickFromRaster_ext_pat2;//ARUint32 + // ext_pat2[AR_PATT_SIZE_Y][AR_PATT_SIZE_X][3]; + int[][] extpat_j; + int[] extpat_j_i; + // int ext_pat2_j[][],ext_pat2_j_i[]; + + initValue_wk_pickFromRaster_ext_pat2(L_extpat, L_WIDTH, L_HEIGHT); + + double[][] cpara_array = i_cpara.getArray(); + double para21_x_yw, para01_x_yw, para11_x_yw; + double para00, para01, para02, para10, para11, para12, para20, para21; + para00 = cpara_array[0 * 3 + 0][0];// para[i][0] = c->m[i*3+0]; + para01 = cpara_array[0 * 3 + 1][0];// para[i][1] = c->m[i*3+1]; + para02 = cpara_array[0 * 3 + 2][0];// para[i][2] = c->m[i*3+2]; + para10 = cpara_array[1 * 3 + 0][0];// para[i][0] = c->m[i*3+0]; + para11 = cpara_array[1 * 3 + 1][0];// para[i][1] = c->m[i*3+1]; + para12 = cpara_array[1 * 3 + 2][0];// para[i][2] = c->m[i*3+2]; + para20 = cpara_array[2 * 3 + 0][0];// para[2][0] = c->m[2*3+0]; + para21 = cpara_array[2 * 3 + 1][0];// para[2][1] = c->m[2*3+1]; + + double d, yw; + int xc, yc; + int i, j; + // arGetCode_put_zero(ext_pat2);//put_zero( (ARUint8 *)ext_pat2, + // AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3*sizeof(ARUint32) ); + int xdiv = i_xdiv2 / L_WIDTH;// xdiv = xdiv2/Config.AR_PATT_SIZE_X; + int ydiv = i_ydiv2 / L_HEIGHT;// ydiv = ydiv2/Config.AR_PATT_SIZE_Y; + + // 計算バッファを予約する + this.reservWorkBuffers(i_xdiv2); + double[] para00_xw = this.wk_updateExtpat_para00_xw; + double[] para10_xw = this.wk_updateExtpat_para10_xw; + double[] para20_xw = this.wk_updateExtpat_para20_xw; + int[] x_rgb_index = this.wk_updateExtpat_x_rgb_index; + int[] y_rgb_index = this.wk_updateExtpat_y_rgb_index; + int[] i_rgb_index = this.wk_updateExtpat_i_rgb_index; + int[] rgb_buf = this.wk_updateExtpat_rgb_buf; + double xw; + for (i = 0; i < i_xdiv2; i++) { + xw = 102.5 + 5.0 * ((double) i + 0.5) / i_xdiv2; + para20_xw[i] = para20 * xw; + para00_xw[i] = para00 * xw; + para10_xw[i] = para10 * xw; + } + + int index_num; + //ピクセルリーダーを取得 + INyARRgbPixelReader reader=image.getRgbPixelReader(); + + for (j = 0; j < i_ydiv2; j++) { + yw = 102.5 + 5.0 * ((double) j + 0.5) / i_ydiv2; + para21_x_yw = para21 * yw + 1.0; + para11_x_yw = para11 * yw + para12; + para01_x_yw = para01 * yw + para02; + extpat_j = L_extpat[j / ydiv]; + index_num = 0; + // ステップ1.RGB取得用のマップを作成 + for (i = 0; i < i_xdiv2; i++) { + d = para20_xw[i] + para21_x_yw; + if (d == 0) { + throw new NyARException(); + } + xc = (int) ((para00_xw[i] + para01_x_yw) / d); + yc = (int) ((para10_xw[i] + para11_x_yw) / d); + // 範囲外は無視 + if (xc < 0 || xc >= img_x || yc < 0 || yc >= img_y) { + continue; + } + // ピクセル値の計算 + // image.getPixel(xc,yc,rgb_buf); + // ext_pat2_j_i=ext_pat2_j[i/xdiv]; + // ext_pat2_j_i[0] += rgb_buf[0];//R + // ext_pat2_j_i[1] += rgb_buf[1];//G + // ext_pat2_j_i[2] += rgb_buf[2];//B + + x_rgb_index[index_num] = xc; + y_rgb_index[index_num] = yc; + i_rgb_index[index_num] = i / xdiv; + index_num++; + } + // //ステップ2.ピクセル配列を取得 + reader.getPixelSet(x_rgb_index, y_rgb_index, index_num, rgb_buf); + // //ピクセル値の計算 + for (i = index_num - 1; i >= 0; i--) { + extpat_j_i = extpat_j[i_rgb_index[i]]; + extpat_j_i[0] += rgb_buf[i * 3 + 0];// R + extpat_j_i[1] += rgb_buf[i * 3 + 1];// G + extpat_j_i[2] += rgb_buf[i * 3 + 2];// B + } + } + /* */ + int xdiv_x_ydiv = xdiv * ydiv; + for (j = L_HEIGHT - 1; j >= 0; j--) { + extpat_j = L_extpat[j]; + for (i = L_WIDTH - 1; i >= 0; i--) { // PRL 2006-06-08. + extpat_j_i = extpat_j[i]; + extpat_j_i[0] /= (xdiv_x_ydiv);// ext_pat[j][i][0] =(byte)(ext_pat2[j][i][0] /(xdiv*ydiv)); + extpat_j_i[1] /= (xdiv_x_ydiv);// ext_pat[j][i][1] =(byte)(ext_pat2[j][i][1] /(xdiv*ydiv)); + extpat_j_i[2] /= (xdiv_x_ydiv);// ext_pat[j][i][2] =(byte)(ext_pat2[j][i][2] /(xdiv*ydiv)); + } + } + return; + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/INyARRaster.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/INyARRaster.java new file mode 100644 index 0000000..fce31d0 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/INyARRaster.java @@ -0,0 +1,46 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster; + + +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.core.rasterreader.*; + +public interface INyARRaster +{ + public int getWidth(); + + public int getHeight(); + + public NyARIntSize getSize(); + public INyARBufferReader getBufferReader(); +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARBinRaster.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARBinRaster.java new file mode 100644 index 0000000..418017e --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARBinRaster.java @@ -0,0 +1,52 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster; + +import jp.nyatla.nyartoolkit.core.rasterreader.*; +import jp.nyatla.nyartoolkit.core.types.*; + +public final class NyARBinRaster extends NyARRaster_BasicClass +{ + private INyARBufferReader _buffer_reader; + protected int[][] _ref_buf; + + public NyARBinRaster(int i_width, int i_height) + { + super(new NyARIntSize(i_width,i_height)); + this._ref_buf = new int[i_height][i_width]; + this._buffer_reader=new NyARBufferReader(this._ref_buf,INyARBufferReader.BUFFERFORMAT_INT2D_BIN_8); + } + public INyARBufferReader getBufferReader() + { + return this._buffer_reader; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARGlayscaleRaster.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARGlayscaleRaster.java new file mode 100644 index 0000000..b828df1 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARGlayscaleRaster.java @@ -0,0 +1,54 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster; + +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.rasterreader.NyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.*; + +public final class NyARGlayscaleRaster extends NyARRaster_BasicClass +{ + + protected int[][] _ref_buf; + private INyARBufferReader _buffer_reader; + + public NyARGlayscaleRaster(int i_width, int i_height) + { + super(new NyARIntSize(i_width,i_height)); + this._ref_buf = new int[i_height][i_width]; + this._buffer_reader=new NyARBufferReader(this._ref_buf,INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8); + } + public INyARBufferReader getBufferReader() + { + return this._buffer_reader; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARRaster_BasicClass.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARRaster_BasicClass.java new file mode 100644 index 0000000..095ee84 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/NyARRaster_BasicClass.java @@ -0,0 +1,58 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster; + +import jp.nyatla.nyartoolkit.core.types.*; + +public abstract class NyARRaster_BasicClass implements INyARRaster +{ + final protected NyARIntSize _size; + protected NyARRaster_BasicClass(final NyARIntSize i_size) + { + this._size= i_size; + } + + final public int getWidth() + { + return this._size.w; + } + + final public int getHeight() + { + return this._size.h; + } + + final public NyARIntSize getSize() + { + return this._size; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/INyARRgbRaster.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/INyARRgbRaster.java new file mode 100644 index 0000000..a3a16fa --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/INyARRgbRaster.java @@ -0,0 +1,45 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster.rgb; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.INyARRaster; +import jp.nyatla.nyartoolkit.core.rasterreader.*; + +/** + * 8bitRGBを表現できるラスタ + * + */ +public interface INyARRgbRaster extends INyARRaster +{ + public INyARRgbPixelReader getRgbPixelReader() throws NyARException; +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_BGRA.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_BGRA.java new file mode 100644 index 0000000..995eb45 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_BGRA.java @@ -0,0 +1,97 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster.rgb; + +import jp.nyatla.nyartoolkit.core.rasterreader.*; +import jp.nyatla.nyartoolkit.core.types.*; + +public class NyARRgbRaster_BGRA extends NyARRgbRaster_BasicClass implements INyARRgbRaster +{ + private class PixelReader implements INyARRgbPixelReader + { + private NyARRgbRaster_BGRA _parent; + + public PixelReader(NyARRgbRaster_BGRA i_parent) + { + this._parent = i_parent; + } + + public void getPixel(int i_x, int i_y, int[] o_rgb) + { + byte[] ref_buf = this._parent._ref_buf; + int bp = (i_x + i_y * this._parent._size.w) * 4; + o_rgb[0] = (ref_buf[bp + 2] & 0xff);// R + o_rgb[1] = (ref_buf[bp + 1] & 0xff);// G + o_rgb[2] = (ref_buf[bp + 0] & 0xff);// B + return; + } + + public void getPixelSet(int[] i_x, int[] i_y, int i_num, int[] o_rgb) + { + int width = _parent._size.w; + byte[] ref_buf = _parent._ref_buf; + int bp; + for (int i = i_num - 1; i >= 0; i--) { + bp = (i_x[i] + i_y[i] * width) * 4; + o_rgb[i * 3 + 0] = (ref_buf[bp + 2] & 0xff);// R + o_rgb[i * 3 + 1] = (ref_buf[bp + 1] & 0xff);// G + o_rgb[i * 3 + 2] = (ref_buf[bp + 0] & 0xff);// B + } + } + } + + private INyARRgbPixelReader _rgb_reader; + private INyARBufferReader _buffer_reader; + private byte[] _ref_buf; + + public static NyARRgbRaster_BGRA wrap(byte[] i_buffer, int i_width, int i_height) + { + return new NyARRgbRaster_BGRA(i_buffer, i_width, i_height); + } + + private NyARRgbRaster_BGRA(byte[] i_buffer, int i_width, int i_height) + { + super(new NyARIntSize(i_width,i_height)); + this._ref_buf = i_buffer; + this._rgb_reader = new PixelReader(this); + this._buffer_reader=new NyARBufferReader(i_buffer,INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32); + return; + } + public INyARRgbPixelReader getRgbPixelReader() + { + return this._rgb_reader; + } + public INyARBufferReader getBufferReader() + { + return this._buffer_reader; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_BasicClass.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_BasicClass.java new file mode 100644 index 0000000..5ef9a16 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_BasicClass.java @@ -0,0 +1,55 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster.rgb; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.NyARRaster_BasicClass; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARRgbPixelReader; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; + +/** + * NyARRasterインタフェイスの基本関数/メンバを実装したクラス + * + * + */ +public abstract class NyARRgbRaster_BasicClass extends NyARRaster_BasicClass implements INyARRgbRaster +{ + protected NyARRgbRaster_BasicClass(final NyARIntSize i_size) + { + super(i_size); + } + public INyARRgbPixelReader getRgbPixcelReader() throws NyARException + { + NyARException.notImplement(); + return null; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_Blank.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_Blank.java new file mode 100644 index 0000000..d7780c4 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_Blank.java @@ -0,0 +1,83 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster.rgb; + +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARRgbPixelReader; +import jp.nyatla.nyartoolkit.core.rasterreader.NyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; + +/* + * 真っ黒の矩形を定義する。 + * + */ +public class NyARRgbRaster_Blank extends NyARRgbRaster_BasicClass +{ + private class PixelReader implements INyARRgbPixelReader + { + public void getPixel(int i_x, int i_y, int[] o_rgb) + { + o_rgb[0] = 0;// R + o_rgb[1] = 0;// G + o_rgb[2] = 0;// B + return; + } + + public void getPixelSet(int[] i_x, int[] i_y, int i_num, int[] o_rgb) + { + for (int i = i_num - 1; i >= 0; i--) { + o_rgb[i * 3 + 0] = 0;// R + o_rgb[i * 3 + 1] = 0;// G + o_rgb[i * 3 + 2] = 0;// B + } + } + } + + private INyARRgbPixelReader _reader; + private INyARBufferReader _buffer_reader; + + public NyARRgbRaster_Blank(int i_width, int i_height) + { + super(new NyARIntSize(i_width,i_height)); + this._reader = new PixelReader(); + this._buffer_reader=new NyARBufferReader(null,INyARBufferReader.BUFFERFORMAT_NULL_ALLZERO); + return; + } + public INyARRgbPixelReader getRgbPixelReader() + { + return this._reader; + } + public INyARBufferReader getBufferReader() + { + return this._buffer_reader; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_RGB.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_RGB.java new file mode 100644 index 0000000..33d6918 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/raster/rgb/NyARRgbRaster_RGB.java @@ -0,0 +1,65 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.raster.rgb; + +import jp.nyatla.nyartoolkit.core.rasterreader.*; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; + +public class NyARRgbRaster_RGB extends NyARRgbRaster_BasicClass +{ + protected byte[] _ref_buf; + + private NyARRgbPixelReader_RGB24 _reader; + private INyARBufferReader _buffer_reader; + + public static NyARRgbRaster_RGB wrap(byte[] i_buffer, int i_width, int i_height) + { + return new NyARRgbRaster_RGB(i_buffer, i_width, i_height); + } + + private NyARRgbRaster_RGB(byte[] i_buffer, int i_width, int i_height) + { + super(new NyARIntSize(i_width,i_height)); + this._ref_buf = i_buffer; + this._reader = new NyARRgbPixelReader_RGB24(i_buffer, this._size); + this._buffer_reader=new NyARBufferReader(i_buffer,INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24); + return; + } + public INyARRgbPixelReader getRgbPixelReader() + { + return this._reader; + } + public INyARBufferReader getBufferReader() + { + return this._buffer_reader; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter.java new file mode 100644 index 0000000..fad5fb0 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter.java @@ -0,0 +1,40 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterfilter; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; + +public interface INyARRasterFilter +{ + public void doFilter(INyARRaster i_input, INyARRaster i_output) throws NyARException; +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_GsToBin.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_GsToBin.java new file mode 100644 index 0000000..29f4920 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_GsToBin.java @@ -0,0 +1,40 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterfilter; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; + +public interface INyARRasterFilter_GsToBin +{ + public void doFilter(NyARGlayscaleRaster i_input, NyARBinRaster i_output) throws NyARException; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_RgbToGs.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_RgbToGs.java new file mode 100644 index 0000000..0fdf48c --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_RgbToGs.java @@ -0,0 +1,45 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterfilter; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster; + +/** + * このインタフェイスは、RGBラスタをグレースケールラスタに変換します。 + * + */ +public interface INyARRasterFilter_RgbToGs +{ + public void doFilter(INyARRgbRaster i_input, NyARGlayscaleRaster i_output) throws NyARException; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/rgb2bin/INyARRasterFilter_RgbToBin.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/rgb2bin/INyARRasterFilter_RgbToBin.java new file mode 100644 index 0000000..f1973bb --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/rgb2bin/INyARRasterFilter_RgbToBin.java @@ -0,0 +1,42 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin; + + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; + +public interface INyARRasterFilter_RgbToBin +{ + public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/rgb2bin/NyARRasterFilter_ARToolkitThreshold.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/rgb2bin/NyARRasterFilter_ARToolkitThreshold.java new file mode 100644 index 0000000..1826a55 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterfilter/rgb2bin/NyARRasterFilter_ARToolkitThreshold.java @@ -0,0 +1,187 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * 定数閾値による2値化をする。 + * + */ +public class NyARRasterFilter_ARToolkitThreshold implements INyARRasterFilter_RgbToBin +{ + private int _threshold; + + public NyARRasterFilter_ARToolkitThreshold(int i_threshold) + { + this._threshold = i_threshold; + } + public void setThreshold(int i_threshold) + { + this._threshold = i_threshold; + } + + public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException + { + INyARBufferReader in_buffer_reader=i_input.getBufferReader(); + INyARBufferReader out_buffer_reader=i_output.getBufferReader(); + int in_buf_type=in_buffer_reader.getBufferType(); + + assert (out_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_BIN_8)); + assert (checkInputType(in_buf_type)==true); + assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); + + int[][] out_buf = (int[][]) out_buffer_reader.getBuffer(); + byte[] in_buf = (byte[]) in_buffer_reader.getBuffer(); + + NyARIntSize size = i_output.getSize(); + switch (in_buffer_reader.getBufferType()) { + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24: + case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24: + convert24BitRgb(in_buf, out_buf, size); + break; + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32: + convert32BitRgbx(in_buf, out_buf, size); + break; + default: + throw new NyARException(); + } + return; + } + + private void convert24BitRgb(byte[] i_in, int[][] i_out, NyARIntSize i_size) + { + final int size_w=i_size.w; + final int x_mod_end= size_w-(size_w%8); + final int th=this._threshold*3; + int bp =(size_w*i_size.h-1)*3; + int w; + int x; + for (int y =i_size.h-1; y>=0 ; y--){ + //端数分 + for (x = size_w-1;x>=x_mod_end;x--) { + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x]=w<=th?0:1; + bp -= 3; + } + //タイリング + for (;x>=0;x-=8) { + w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x]=w<=th?0:1; + bp -= 3; + w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-1]=w<=th?0:1; + bp -= 3; + w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-2]=w<=th?0:1; + bp -= 3; + w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-3]=w<=th?0:1; + bp -= 3; + w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-4]=w<=th?0:1; + bp -= 3; + w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-5]=w<=th?0:1; + bp -= 3; + w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-6]=w<=th?0:1; + bp -= 3; + w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-7]=w<=th?0:1; + bp -= 3; + } + } + return; + } + private void convert32BitRgbx(byte[] i_in, int[][] i_out, NyARIntSize i_size) + { + final int size_w=i_size.w; + final int x_mod_end= size_w-(size_w%8); + final int th=this._threshold*3; + int bp =(size_w*i_size.h-1)*4; + int w; + int x; + for (int y =i_size.h-1; y>=0 ; y--){ + //端数分 + for (x = size_w-1;x>=x_mod_end;x--) { + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x]=w<=th?0:1; + bp -= 4; + } + //タイリング + for (;x>=0;x-=8) { + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x]=w<=th?0:1; + bp -= 4; + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-1]=w<=th?0:1; + bp -= 4; + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-2]=w<=th?0:1; + bp -= 4; + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-3]=w<=th?0:1; + bp -= 4; + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-4]=w<=th?0:1; + bp -= 4; + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-5]=w<=th?0:1; + bp -= 4; + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-6]=w<=th?0:1; + bp -= 4; + w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)); + i_out[y][x-7]=w<=th?0:1; + bp -= 4; + } + } + return; + } + + private boolean checkInputType(int i_input_type) throws NyARException + { + switch(i_input_type){ + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24: + case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24: + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32: + return true; + default: + return false; + } + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/INyARBufferReader.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/INyARBufferReader.java new file mode 100644 index 0000000..24f1815 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/INyARBufferReader.java @@ -0,0 +1,74 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterreader; + +public interface INyARBufferReader +{ + /** + * RGB24フォーマットで、全ての画素が0 + */ + public static final int BUFFERFORMAT_NULL_ALLZERO = 0x00000001; + + /** + * byte[]で、R8G8B8の24ビットで画素が格納されている。 + */ + public static final int BUFFERFORMAT_BYTE1D_R8G8B8_24 = 0x00010001; + + /** + * byte[]で、B8G8R8の24ビットで画素が格納されている。 + */ + public static final int BUFFERFORMAT_BYTE1D_B8G8R8_24 = 0x00010002; + + /** + * byte[]で、R8G8B8X8の32ビットで画素が格納されている。 + */ + public static final int BUFFERFORMAT_BYTE1D_B8G8R8X8_32 = 0x00010101; + + /** + * int[][]で特に値範囲を定めない + */ + public static final int BUFFERFORMAT_INT2D = 0x00020000; + + /** + * int[][]で0-255のグレイスケール画像 + */ + public static final int BUFFERFORMAT_INT2D_GLAY_8 = 0x00020001; + + /** + * int[][]で0/1の2値画像 + */ + public static final int BUFFERFORMAT_INT2D_BIN_8 = 0x00020002; + + public Object getBuffer(); + public int getBufferType(); + public boolean isEqualBufferType(int i_type_value); +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/INyARRgbPixelReader.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/INyARRgbPixelReader.java new file mode 100644 index 0000000..71b8c9c --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/INyARRgbPixelReader.java @@ -0,0 +1,64 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterreader; + +import jp.nyatla.nyartoolkit.NyARException; + +/** + * R8G8B8でピクセルを読み出すインタフェイス + * + */ +public interface INyARRgbPixelReader +{ + /** + * 1ピクセルをint配列にして返します。 + * + * @param i_x + * @param i_y + * @param i_rgb + */ + public void getPixel(int i_x, int i_y, int[] i_rgb) throws NyARException; + + /** + * 複数のピクセル値をi_rgbへ返します。 + * + * @param i_x + * xのインデックス配列 + * @param i_y + * yのインデックス配列 + * @param i_num + * 返すピクセル値の数 + * @param i_rgb + * ピクセル値を返すバッファ + */ + public void getPixelSet(int[] i_x, int[] i_y, int i_num, int[] i_rgb) throws NyARException; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/NyARBufferReader.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/NyARBufferReader.java new file mode 100644 index 0000000..dbbc5d1 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/NyARBufferReader.java @@ -0,0 +1,55 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterreader; + +public class NyARBufferReader implements INyARBufferReader +{ + protected Object _buffer; + protected int _buffer_type; + public NyARBufferReader(Object i_buffer,int i_buffer_type) + { + this._buffer=i_buffer; + this._buffer_type=i_buffer_type; + } + public Object getBuffer() + { + return this._buffer; + } + public int getBufferType() + { + return _buffer_type; + } + public boolean isEqualBufferType(int i_type_value) + { + return this._buffer_type==i_type_value; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/NyARRgbPixelReader_RGB24.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/NyARRgbPixelReader_RGB24.java new file mode 100644 index 0000000..86d3dba --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/rasterreader/NyARRgbPixelReader_RGB24.java @@ -0,0 +1,74 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.rasterreader; + +import jp.nyatla.nyartoolkit.core.types.*; +/** + * byte[]配列に、パディング無しの8bit画素値が、RGBRGBの順で並んでいる + * バッファに使用できるピクセルリーダー + * + */ +public class NyARRgbPixelReader_RGB24 implements INyARRgbPixelReader +{ + protected byte[] _ref_buf; + + private NyARIntSize _size; + + public NyARRgbPixelReader_RGB24(byte[] i_buf, NyARIntSize i_size) + { + this._ref_buf = i_buf; + this._size = i_size; + } + + public void getPixel(int i_x, int i_y, int[] o_rgb) + { + byte[] ref_buf = this._ref_buf; + int bp = (i_x + i_y * this._size.w) * 3; + o_rgb[0] = (ref_buf[bp + 0] & 0xff);// R + o_rgb[1] = (ref_buf[bp + 1] & 0xff);// G + o_rgb[2] = (ref_buf[bp + 2] & 0xff);// B + return; + } + + public void getPixelSet(int[] i_x, int[] i_y, int i_num, int[] o_rgb) + { + int width = this._size.w; + byte[] ref_buf = this._ref_buf; + int bp; + for (int i = i_num - 1; i >= 0; i--) { + bp = (i_x[i] + i_y[i] * width) * 3; + o_rgb[i * 3 + 0] = (ref_buf[bp + 0] & 0xff);// R + o_rgb[i * 3 + 1] = (ref_buf[bp + 1] & 0xff);// G + o_rgb[i * 3 + 2] = (ref_buf[bp + 2] & 0xff);// B + } + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/INyARTransMat.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/INyARTransMat.java new file mode 100644 index 0000000..f90ba47 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/INyARTransMat.java @@ -0,0 +1,47 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARSquare; + + +/** + * This class calculates ARMatrix from square information. -- 変換行列を計算するクラス。 + * + */ +public interface INyARTransMat +{ + public void setCenter(double i_x, double i_y); + public void transMat(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result) throws NyARException; + public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult io_result_conv) throws NyARException; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java new file mode 100644 index 0000000..067818f --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java @@ -0,0 +1,204 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARSquare; +import jp.nyatla.nyartoolkit.core.param.*; +import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator; +import jp.nyatla.nyartoolkit.core.transmat.optimize.NyARRotTransOptimize; +import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; +import jp.nyatla.nyartoolkit.core.types.*; + + +/** + * This class calculates ARMatrix from square information and holds it. -- + * 変換行列を計算して、結果を保持するクラス。 + * + */ +public class NyARTransMat implements INyARTransMat +{ + private final static double AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR = 1.0; + + private final NyARRotMatrix _rotmatrix; + private final NyARDoublePoint2d _center=new NyARDoublePoint2d(0,0); + private final NyARFitVecCalculator _calculator; + private final NyARTransOffset _offset=new NyARTransOffset(); + private final NyARRotTransOptimize _mat_optimize; + + + public NyARTransMat(NyARParam i_param) throws NyARException + { + final NyARCameraDistortionFactor dist=i_param.getDistortionFactor(); + final NyARPerspectiveProjectionMatrix pmat=i_param.getPerspectiveProjectionMatrix(); + this._calculator=new NyARFitVecCalculator(pmat,dist); + this._rotmatrix = new NyARRotMatrix(pmat); + this._mat_optimize=new NyARRotTransOptimize(pmat); + } + + public void setCenter(double i_x, double i_y) + { + this._center.x= i_x; + this._center.y= i_y; + } + + + + + /** + * 頂点順序をi_directionに対応して並べ替えます。 + * @param i_square + * @param i_direction + * @param o_sqvertex_ref + * @param o_liner_ref + */ + private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref) + { + //頂点順序を考慮した矩形の頂点情報 + o_sqvertex_ref[0]= i_square.sqvertex[(4 - i_direction) % 4]; + o_sqvertex_ref[1]= i_square.sqvertex[(5 - i_direction) % 4]; + o_sqvertex_ref[2]= i_square.sqvertex[(6 - i_direction) % 4]; + o_sqvertex_ref[3]= i_square.sqvertex[(7 - i_direction) % 4]; + o_liner_ref[0]=i_square.line[(4 - i_direction) % 4]; + o_liner_ref[1]=i_square.line[(5 - i_direction) % 4]; + o_liner_ref[2]=i_square.line[(6 - i_direction) % 4]; + o_liner_ref[3]=i_square.line[(7 - i_direction) % 4]; + return; + } + + + private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4]; + private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4]; + private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d(); + /** + * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] ) + * + * @param i_square + * 計算対象のNyARSquareオブジェクト + * @param i_direction + * @param i_width + * @return + * @throws NyARException + */ + public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException + { + final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref; + final NyARLinear[] linear_ref=__transMat_linear_ref; + final NyARDoublePoint3d trans=this.__transMat_trans; + + //計算用に頂点情報を初期化(順番調整) + initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref); + + //基準矩形を設定 + this._offset.setSquare(i_width,this._center); + + // rotationを矩形情報から計算 + this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref); + + //平行移動量計算機にオフセット頂点をセット + this._calculator.setOffsetSquare(this._offset); + + //平行移動量計算機に適応先矩形の情報をセット + this._calculator.setFittedSquare(sqvertex_ref); + + //回転行列の平行移動量の計算 + this._calculator.calculateTransfer(this._rotmatrix,trans); + + //計算結果の最適化(this._rotmatrix,trans) + this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator); + + // マトリクスの保存 + o_result_conv.updateMatrixValue(this._rotmatrix, this._offset.point, trans); + return; + } + /** + * double arGetTransMatCont( ARMarkerInfo *marker_info, double prev_conv[3][4],double center[2], double width, double conv[3][4] ) + * + * @param i_square + * @param i_direction + * マーカーの方位を指定する。 + * @param i_width + * @param io_result_conv + * 計算履歴を持つNyARTransMatResultオブジェクトを指定する。 履歴を持たない場合は、transMatと同じ処理を行う。 + * @return + * @throws NyARException + */ + public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult io_result_conv) throws NyARException + { + final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref; + final NyARLinear[] linear_ref=__transMat_linear_ref; + final NyARDoublePoint3d trans=this.__transMat_trans; + + // io_result_convが初期値なら、transMatで計算する。 + if (!io_result_conv.hasValue()) { + this.transMat(i_square, i_direction, i_width, io_result_conv); + return; + } + + //基準矩形を設定 + this._offset.setSquare(i_width,this._center); + + // rotationを矩形情報を一つ前の変換行列で初期化 + this._rotmatrix.initRotByPrevResult(io_result_conv); + + //平行移動量計算機に、オフセット頂点をセット + this._calculator.setOffsetSquare(this._offset); + + //平行移動量計算機に、適応先矩形の情報をセット + this._calculator.setFittedSquare(sqvertex_ref); + + //回転行列の平行移動量の計算 + this._calculator.calculateTransfer(this._rotmatrix,trans); + + //計算結果の最適化(this._rotmatrix,trans) + final double err=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator); + + //計算結果を保存 + io_result_conv.updateMatrixValue(this._rotmatrix, this._offset.point, trans); + + // エラー値が許容範囲でなければTransMatをやり直し + if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) { + // rotationを矩形情報で初期化 + this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref); + //回転行列の平行移動量の計算 + this._calculator.calculateTransfer(this._rotmatrix,trans); + //計算結果の最適化(this._rotmatrix,trans) + final double err2=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator); + //エラー値が低かったら値を差換え + if (err2 < err) { + // 良い値が取れたら、差換え + io_result_conv.updateMatrixValue(this._rotmatrix, this._offset.point, trans); + } + } + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMatResult.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMatResult.java new file mode 100644 index 0000000..1c962c3 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMatResult.java @@ -0,0 +1,80 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat; + + +import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.core.types.matrix.*; + +/** + * NyARTransMat戻り値専用のNyARMat + * + */ +public class NyARTransMatResult extends NyARDoubleMatrix34 +{ + private boolean has_value = false; + + + /** + * パラメータで変換行列を更新します。 + * + * @param i_rot + * @param i_off + * @param i_trans + */ + public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans) + { + this.m00=i_rot.m00; + this.m01=i_rot.m01; + this.m02=i_rot.m02; + this.m03=i_rot.m00 * i_off.x + i_rot.m01 * i_off.y + i_rot.m02 * i_off.z + i_trans.x; + + this.m10 = i_rot.m10; + this.m11 = i_rot.m11; + this.m12 = i_rot.m12; + this.m13 = i_rot.m10 * i_off.x + i_rot.m11 * i_off.y + i_rot.m12 * i_off.z + i_trans.y; + + this.m20 = i_rot.m20; + this.m21 = i_rot.m21; + this.m22 = i_rot.m22; + this.m23 = i_rot.m20 * i_off.x + i_rot.m21 * i_off.y + i_rot.m22 * i_off.z + i_trans.z; + + this.has_value = true; + return; + } + + public boolean hasValue() + { + return this.has_value; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransOffset.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransOffset.java new file mode 100644 index 0000000..bc776fb --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransOffset.java @@ -0,0 +1,73 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat; + +import jp.nyatla.nyartoolkit.core.types.*; + + +final public class NyARTransOffset +{ + public NyARDoublePoint3d[] vertex=NyARDoublePoint3d.createArray(4); + public NyARDoublePoint3d point=new NyARDoublePoint3d(); + /** + * 中心位置と辺長から、オフセット情報を作成して設定する。 + * @param i_width + * @param i_center + */ + public void setSquare(double i_width,NyARDoublePoint2d i_center) + { + final double w_2 = i_width / 2.0; + + NyARDoublePoint3d vertex3d_ptr; + vertex3d_ptr= this.vertex[0]; + vertex3d_ptr.x = -w_2; + vertex3d_ptr.y = w_2; + vertex3d_ptr.z = 0.0; + vertex3d_ptr= this.vertex[1]; + vertex3d_ptr.x = w_2; + vertex3d_ptr.y = w_2; + vertex3d_ptr.z = 0.0; + vertex3d_ptr= this.vertex[2]; + vertex3d_ptr.x = w_2; + vertex3d_ptr.y = -w_2; + vertex3d_ptr.z = 0.0; + vertex3d_ptr= this.vertex[3]; + vertex3d_ptr.x = -w_2; + vertex3d_ptr.y = -w_2; + vertex3d_ptr.z = 0.0; + + this.point.x=-i_center.x; + this.point.y=-i_center.y; + this.point.z=0; + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/fitveccalc/NyARFitVecCalculator.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/fitveccalc/NyARFitVecCalculator.java new file mode 100644 index 0000000..5830d02 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/fitveccalc/NyARFitVecCalculator.java @@ -0,0 +1,181 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat.fitveccalc; + +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.transmat.NyARTransOffset; +import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.*; +import jp.nyatla.nyartoolkit.core.param.*; +/** + * 平行移動量を計算するクラス + * + * NyARPerspectiveProjectionMatrixに直接アクセスしてる場所があるけど、 + * この辺の計算はNyARPerspectiveProjectionMatrixクラスの関数にして押し込む予定。 + * + */ +public class NyARFitVecCalculator +{ + private final NyARMat _mat_b = new NyARMat(3,8);//3,NUMBER_OF_VERTEX*2 + private final NyARMat _mat_a = new NyARMat(8,3);/*NUMBER_OF_VERTEX,3*/ + private final NyARMat _mat_d = new NyARMat(3,3); + private final NyARPerspectiveProjectionMatrix _projection_mat; + private final NyARCameraDistortionFactor _distortionfactor; + + +// private NyARDoublePoint2d[] _vertex_2d_ref; + public NyARFitVecCalculator(final NyARPerspectiveProjectionMatrix i_projection_mat_ref,final NyARCameraDistortionFactor i_distortion_ref) + { + // 変換マトリクスdとbの準備(arGetTransMatSubの一部) + final double[][] a_array = this._mat_a.getArray(); + final double[][] b_array = this._mat_b.getArray(); + + //変換用行列のcpara固定値の部分を先に初期化してしまう。 + for (int i = 0; i < 4; i++) { + final int x2 = i * 2; + a_array[x2][0] = b_array[0][x2] = i_projection_mat_ref.m00;// mat_a->m[j*6+0]=mat_b->m[num*0+j*2] =cpara[0][0]; + a_array[x2][1] = b_array[1][x2] = i_projection_mat_ref.m01;// mat_a->m[j*6+1]=mat_b->m[num*2+j*2]=cpara[0][1]; + //a_array[x2][2] = b_array[2][x2] = cpara[0 * 4 + 2] - o_marker_vertex_2d[i].x;// mat_a->m[j*6+2]=mat_b->m[num*4+j*2]=cpara[0][2]-pos2d[j][0]; + a_array[x2 + 1][0] = b_array[0][x2 + 1] = 0.0;// mat_a->m[j*6+3] =mat_b->m[num*0+j*2+1]= 0.0; + a_array[x2 + 1][1] = b_array[1][x2 + 1] = i_projection_mat_ref.m11;// mat_a->m[j*6+4] =mat_b->m[num*2+j*2+1]= cpara[1][1]; + //a_array[x2 + 1][2] = b_array[2][x2 + 1] = cpara[1 * 4 + 2] - o_marker_vertex_2d[i].y;// mat_a->m[j*6+5]=mat_b->m[num*4+j*2+1]=cpara[1][2]-pos2d[j][1]; + } + this._projection_mat=i_projection_mat_ref; + this._distortionfactor=i_distortion_ref; + return; + } + private final NyARDoublePoint2d[] _fitsquare_vertex=NyARDoublePoint2d.createArray(4);; + private NyARTransOffset _offset_square; + public void setOffsetSquare(NyARTransOffset i_offset) + { + this._offset_square=i_offset; + return; + } + public NyARDoublePoint2d[] getFitSquare() + { + return this._fitsquare_vertex; + } + public NyARTransOffset getOffsetVertex() + { + return this._offset_square; + } + + /** + * 適合させる矩形座標を指定します。 + * @param i_square_vertex + * @throws NyARException + */ + public void setFittedSquare(NyARDoublePoint2d[] i_square_vertex) throws NyARException + { + final NyARDoublePoint2d[] vertex=_fitsquare_vertex; +// int i; +// if (arFittingMode == AR_FITTING_TO_INPUT) { +// // arParamIdeal2Observをバッチ処理 + this._distortionfactor.ideal2ObservBatch(i_square_vertex, vertex,4); +// } else { +// for (i = 0; i < NUMBER_OF_VERTEX; i++) { +// o_marker_vertex_2d[i].x = i_square_vertex[i].x; +// o_marker_vertex_2d[i].y = i_square_vertex[i].y; +// } +// } + + + final double cpara02=this._projection_mat.m02; + final double cpara12=this._projection_mat.m12; + final NyARMat mat_d=_mat_d; + final NyARMat mat_a=this._mat_a; + final NyARMat mat_b=this._mat_b; + final double[][] a_array = mat_a.getArray(); + final double[][] b_array = mat_b.getArray(); + for (int i = 0; i < 4; i++) { + final int x2 = i * 2; + a_array[x2][2] = b_array[2][x2] = cpara02 - vertex[i].x;// mat_a->m[j*6+2]=mat_b->m[num*4+j*2]=cpara[0][2]-pos2d[j][0]; + a_array[x2 + 1][2] = b_array[2][x2 + 1] = cpara12 - vertex[i].y;// mat_a->m[j*6+5]=mat_b->m[num*4+j*2+1]=cpara[1][2]-pos2d[j][1]; + } + // mat_d + mat_d.matrixMul(mat_b, mat_a); + mat_d.matrixSelfInv(); + return; + } + private final NyARMat _mat_e = new NyARMat(3, 1); + private final NyARMat _mat_f = new NyARMat(3, 1); + private final NyARMat __calculateTransferVec_mat_c = new NyARMat(8, 1);//NUMBER_OF_VERTEX * 2, 1 + private final NyARDoublePoint3d[] __calculateTransfer_point3d=NyARDoublePoint3d.createArray(4); + + /** + * 現在のオフセット矩形、適合先矩形と、回転行列から、平行移動量を計算します。 + * @param i_rotation + * @param o_transfer + * @throws NyARException + */ + final public void calculateTransfer(NyARRotMatrix i_rotation,NyARDoublePoint3d o_transfer) throws NyARException + { + assert(this._offset_square!=null); + final double cpara00=this._projection_mat.m00; + final double cpara01=this._projection_mat.m01; + final double cpara02=this._projection_mat.m02; + final double cpara11=this._projection_mat.m11; + final double cpara12=this._projection_mat.m12; + + final NyARDoublePoint3d[] point3d=this.__calculateTransfer_point3d; + final NyARDoublePoint3d[] vertex3d=this._offset_square.vertex; + final NyARDoublePoint2d[] vertex2d=this._fitsquare_vertex; + final NyARMat mat_c = this.__calculateTransferVec_mat_c;// 次処理で値をもらうので、初期化の必要は無い。 + + final double[][] f_array = this._mat_f.getArray(); + final double[][] c_array = mat_c.getArray(); + + + //(3D座標?)を一括請求 + i_rotation.getPoint3dBatch(vertex3d,point3d,4); + for (int i = 0; i < 4; i++) { + final int x2 = i+i; + final NyARDoublePoint3d point3d_ptr=point3d[i]; +// i_rotation.getPoint3d(vertex3d[i],point3d); + //透視変換? + c_array[x2][0] = point3d_ptr.z * vertex2d[i].x - cpara00 * point3d_ptr.x - cpara01 * point3d_ptr.y - cpara02 * point3d_ptr.z;// mat_c->m[j*2+0] = wz*pos2d[j][0]-cpara[0][0]*wx-cpara[0][1]*wy-cpara[0][2]*wz; + c_array[x2 + 1][0] = point3d_ptr.z * vertex2d[i].y - cpara11 * point3d_ptr.y - cpara12 * point3d_ptr.z;// mat_c->m[j*2+1]= wz*pos2d[j][1]-cpara[1][1]*wy-cpara[1][2]*wz; + } + this._mat_e.matrixMul(this._mat_b, mat_c); + this._mat_f.matrixMul(this._mat_d, this._mat_e); + + // double[] trans=wk_arGetTransMatSub_trans;//double trans[3]; + o_transfer.x= f_array[0][0];// trans[0] = mat_f->m[0]; + o_transfer.y= f_array[1][0]; + o_transfer.z= f_array[2][0];// trans[2] = mat_f->m[2]; + return; + } + + + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotTransOptimize.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotTransOptimize.java new file mode 100644 index 0000000..879b9a7 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotTransOptimize.java @@ -0,0 +1,43 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat.optimize; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator; +import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; + +public interface INyARRotTransOptimize +{ + public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,NyARFitVecCalculator i_calculator) throws NyARException; + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize.java new file mode 100644 index 0000000..27aff2f --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize.java @@ -0,0 +1,292 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat.optimize; + + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.param.*; +import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator; +import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; +/** + * 基本姿勢と実画像を一致するように、角度を微調整→平行移動量を再計算 + * を繰り返して、変換行列を最適化する。 + * + */ +public class NyARRotTransOptimize implements INyARRotTransOptimize +{ + private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5 + private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0 + private final NyARPerspectiveProjectionMatrix _projection_mat_ref; + public NyARRotTransOptimize(NyARPerspectiveProjectionMatrix i_projection_mat_ref) + { + this._projection_mat_ref=i_projection_mat_ref; + return; + } + + final public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,NyARFitVecCalculator i_calculator) throws NyARException + { + final NyARDoublePoint2d[] fit_vertex=i_calculator.getFitSquare(); + final NyARDoublePoint3d[] offset_square=i_calculator.getOffsetVertex().vertex; + + double err = -1; + /*ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。*/ + for (int i = 0;; i++) { + // + err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex); + i_calculator.calculateTransfer(io_rotmat, io_transvec); + err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex); + // // + if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT-1) { + break; + } + i_calculator.calculateTransfer(io_rotmat, io_transvec); + } + return err; + } + + private final double[][] __modifyMatrix_double1D = new double[8][3]; + private final NyARDoublePoint3d __modifyMatrix_angle = new NyARDoublePoint3d(); + /** + * arGetRot計算を階層化したModifyMatrix 896 + * + * @param nyrot + * @param trans + * @param i_vertex3d + * [m][3] + * @param i_vertex2d + * [n][2] + * @return + * @throws NyARException + */ + private double modifyMatrix(NyARRotMatrix io_rot,NyARDoublePoint3d trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException + { + double factor; + double a2, b2, c2; + double ma = 0.0, mb = 0.0, mc = 0.0; + double h, x, y; + double err, minerr = 0; + int t1, t2, t3; + int s1 = 0, s2 = 0, s3 = 0; + + factor = 10.0 * Math.PI / 180.0; + double rot0, rot1, rot3, rot4, rot6, rot7; + double combo00, combo01, combo02, combo03, combo10, combo11, combo12, combo13, combo20, combo21, combo22, combo23; + double combo02_2, combo02_5, combo02_8, combo02_11; + double combo22_2, combo22_5, combo22_8, combo22_11; + double combo12_2, combo12_5, combo12_8, combo12_11; + // vertex展開 + final double VX00, VX01, VX02, VX10, VX11, VX12, VX20, VX21, VX22, VX30, VX31, VX32; + NyARDoublePoint3d d_pt; + d_pt = i_vertex3d[0]; + VX00 = d_pt.x; + VX01 = d_pt.y; + VX02 = d_pt.z; + d_pt = i_vertex3d[1]; + VX10 = d_pt.x; + VX11 = d_pt.y; + VX12 = d_pt.z; + d_pt = i_vertex3d[2]; + VX20 = d_pt.x; + VX21 = d_pt.y; + VX22 = d_pt.z; + d_pt = i_vertex3d[3]; + VX30 = d_pt.x; + VX31 = d_pt.y; + VX32 = d_pt.z; + final double P2D00, P2D01, P2D10, P2D11, P2D20, P2D21, P2D30, P2D31; + NyARDoublePoint2d d_pt2; + d_pt2 = i_vertex2d[0]; + P2D00 = d_pt2.x; + P2D01 = d_pt2.y; + d_pt2 = i_vertex2d[1]; + P2D10 = d_pt2.x; + P2D11 = d_pt2.y; + d_pt2 = i_vertex2d[2]; + P2D20 = d_pt2.x; + P2D21 = d_pt2.y; + d_pt2 = i_vertex2d[3]; + P2D30 = d_pt2.x; + P2D31 = d_pt2.y; + final NyARPerspectiveProjectionMatrix prjmat = this._projection_mat_ref; + final double CP0, CP1, CP2, CP4, CP5, CP6, CP8, CP9, CP10; + CP0 = prjmat.m00; + CP1 = prjmat.m01; + CP2 = prjmat.m02; + CP4 = prjmat.m10; + CP5 = prjmat.m11; + CP6 = prjmat.m12; + CP8 = prjmat.m20; + CP9 = prjmat.m21; + CP10 = prjmat.m22; + combo03 = CP0 * trans.x + CP1 * trans.y + CP2 * trans.z + prjmat.m03; + combo13 = CP4 * trans.x + CP5 * trans.y + CP6 * trans.z + prjmat.m13; + combo23 = CP8 * trans.x + CP9 * trans.y + CP10 * trans.z + prjmat.m23; + double CACA, SASA, SACA, CA, SA; + double CACACB, SACACB, SASACB, CASB, SASB; + double SACASC, SACACBSC, SACACBCC, SACACC; + final double[][] double1D = this.__modifyMatrix_double1D; + + final NyARDoublePoint3d angle = this.__modifyMatrix_angle; + + final double[] a_factor = double1D[1]; + final double[] sinb = double1D[2]; + final double[] cosb = double1D[3]; + final double[] b_factor = double1D[4]; + final double[] sinc = double1D[5]; + final double[] cosc = double1D[6]; + final double[] c_factor = double1D[7]; + double w, w2; + double wsin, wcos; + + io_rot.getAngle(angle);// arGetAngle( rot, &a, &b, &c ); + a2 = angle.x; + b2 = angle.y; + c2 = angle.z; + + // comboの3行目を先に計算 + for (int i = 0; i < 10; i++) { + minerr = 1000000000.0; + // sin-cosテーブルを計算(これが外に出せるとは…。) + for (int j = 0; j < 3; j++) { + w2 = factor * (j - 1); + w = a2 + w2; + a_factor[j] = w; + w = b2 + w2; + b_factor[j] = w; + sinb[j] = Math.sin(w); + cosb[j] = Math.cos(w); + w = c2 + w2; + c_factor[j] = w; + sinc[j] = Math.sin(w); + cosc[j] = Math.cos(w); + } + // + for (t1 = 0; t1 < 3; t1++) { + SA = Math.sin(a_factor[t1]); + CA = Math.cos(a_factor[t1]); + // Optimize + CACA = CA * CA; + SASA = SA * SA; + SACA = SA * CA; + for (t2 = 0; t2 < 3; t2++) { + wsin = sinb[t2]; + wcos = cosb[t2]; + CACACB = CACA * wcos; + SACACB = SACA * wcos; + SASACB = SASA * wcos; + CASB = CA * wsin; + SASB = SA * wsin; + // comboの計算1 + combo02 = CP0 * CASB + CP1 * SASB + CP2 * wcos; + combo12 = CP4 * CASB + CP5 * SASB + CP6 * wcos; + combo22 = CP8 * CASB + CP9 * SASB + CP10 * wcos; + + combo02_2 = combo02 * VX02 + combo03; + combo02_5 = combo02 * VX12 + combo03; + combo02_8 = combo02 * VX22 + combo03; + combo02_11 = combo02 * VX32 + combo03; + combo12_2 = combo12 * VX02 + combo13; + combo12_5 = combo12 * VX12 + combo13; + combo12_8 = combo12 * VX22 + combo13; + combo12_11 = combo12 * VX32 + combo13; + combo22_2 = combo22 * VX02 + combo23; + combo22_5 = combo22 * VX12 + combo23; + combo22_8 = combo22 * VX22 + combo23; + combo22_11 = combo22 * VX32 + combo23; + for (t3 = 0; t3 < 3; t3++) { + wsin = sinc[t3]; + wcos = cosc[t3]; + SACASC = SACA * wsin; + SACACC = SACA * wcos; + SACACBSC = SACACB * wsin; + SACACBCC = SACACB * wcos; + + rot0 = CACACB * wcos + SASA * wcos + SACACBSC - SACASC; + rot3 = SACACBCC - SACACC + SASACB * wsin + CACA * wsin; + rot6 = -CASB * wcos - SASB * wsin; + + combo00 = CP0 * rot0 + CP1 * rot3 + CP2 * rot6; + combo10 = CP4 * rot0 + CP5 * rot3 + CP6 * rot6; + combo20 = CP8 * rot0 + CP9 * rot3 + CP10 * rot6; + + rot1 = -CACACB * wsin - SASA * wsin + SACACBCC - SACACC; + rot4 = -SACACBSC + SACASC + SASACB * wcos + CACA * wcos; + rot7 = CASB * wsin - SASB * wcos; + combo01 = CP0 * rot1 + CP1 * rot4 + CP2 * rot7; + combo11 = CP4 * rot1 + CP5 * rot4 + CP6 * rot7; + combo21 = CP8 * rot1 + CP9 * rot4 + CP10 * rot7; + // + err = 0.0; + h = combo20 * VX00 + combo21 * VX01 + combo22_2; + x = P2D00 - (combo00 * VX00 + combo01 * VX01 + combo02_2) / h; + y = P2D01 - (combo10 * VX00 + combo11 * VX01 + combo12_2) / h; + err += x * x + y * y; + h = combo20 * VX10 + combo21 * VX11 + combo22_5; + x = P2D10 - (combo00 * VX10 + combo01 * VX11 + combo02_5) / h; + y = P2D11 - (combo10 * VX10 + combo11 * VX11 + combo12_5) / h; + err += x * x + y * y; + h = combo20 * VX20 + combo21 * VX21 + combo22_8; + x = P2D20 - (combo00 * VX20 + combo01 * VX21 + combo02_8) / h; + y = P2D21 - (combo10 * VX20 + combo11 * VX21 + combo12_8) / h; + err += x * x + y * y; + h = combo20 * VX30 + combo21 * VX31 + combo22_11; + x = P2D30 - (combo00 * VX30 + combo01 * VX31 + combo02_11) / h; + y = P2D31 - (combo10 * VX30 + combo11 * VX31 + combo12_11) / h; + err += x * x + y * y; + if (err < minerr) { + minerr = err; + ma = a_factor[t1]; + mb = b_factor[t2]; + mc = c_factor[t3]; + s1 = t1 - 1; + s2 = t2 - 1; + s3 = t3 - 1; + } + } + } + } + if (s1 == 0 && s2 == 0 && s3 == 0) { + factor *= 0.5; + } + a2 = ma; + b2 = mb; + c2 = mc; + } + io_rot.setAngle(ma, mb, mc); + /* printf("factor = %10.5f\n", factor*180.0/MD_PI); */ + return minerr / 4; + } + + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/rotmatrix/NyARRotMatrix.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/rotmatrix/NyARRotMatrix.java new file mode 100644 index 0000000..a105225 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/rotmatrix/NyARRotMatrix.java @@ -0,0 +1,302 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat.rotmatrix; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix33; +import jp.nyatla.nyartoolkit.core.param.*; +/** + * 回転行列計算用の、3x3行列 + * + */ +public class NyARRotMatrix extends NyARDoubleMatrix33 +{ + /** + * インスタンスを準備します。 + * + * @param i_param + */ + public NyARRotMatrix(NyARPerspectiveProjectionMatrix i_matrix) throws NyARException + { + this.__initRot_vec1=new NyARRotVector(i_matrix); + this.__initRot_vec2=new NyARRotVector(i_matrix); + return; + } + final private NyARRotVector __initRot_vec1; + final private NyARRotVector __initRot_vec2; + + + + public final void initRotByPrevResult(NyARTransMatResult i_prev_result) + { + + this.m00=i_prev_result.m00; + this.m01=i_prev_result.m01; + this.m02=i_prev_result.m02; + + this.m10=i_prev_result.m10; + this.m11=i_prev_result.m11; + this.m12=i_prev_result.m12; + + this.m20=i_prev_result.m20; + this.m21=i_prev_result.m21; + this.m22=i_prev_result.m22; + return; + } + + + public final void initRotBySquare(final NyARLinear[] i_linear,final NyARDoublePoint2d[] i_sqvertex) throws NyARException + { + final NyARRotVector vec1=this.__initRot_vec1; + final NyARRotVector vec2=this.__initRot_vec2; + + //向かい合った辺から、2本のベクトルを計算 + + //軸1 + vec1.exteriorProductFromLinear(i_linear[0], i_linear[2]); + vec1.checkVectorByVertex(i_sqvertex[0], i_sqvertex[1]); + + //軸2 + vec2.exteriorProductFromLinear(i_linear[1], i_linear[3]); + vec2.checkVectorByVertex(i_sqvertex[3], i_sqvertex[0]); + + //回転の最適化? + NyARRotVector.checkRotation(vec1,vec2); + + this.m00 =vec1.v1; + this.m10 =vec1.v2; + this.m20 =vec1.v3; + this.m01 =vec2.v1; + this.m11 =vec2.v2; + this.m21 =vec2.v3; + + //最後の軸を計算 + final double w02 = vec1.v2 * vec2.v3 - vec1.v3 * vec2.v2; + final double w12 = vec1.v3 * vec2.v1 - vec1.v1 * vec2.v3; + final double w22 = vec1.v1 * vec2.v2 - vec1.v2 * vec2.v1; + final double w = Math.sqrt(w02 * w02 + w12 * w12 + w22 * w22); + this.m02 = w02/w; + this.m12 = w12/w; + this.m22 = w22/w; + return; + } + + + + /** + * int arGetAngle( double rot[3][3], double *wa, double *wb, double *wc ) + * Optimize:2008.04.20:STEP[481→433] + * 3x3変換行列から、回転角を復元して返します。 + * @param o_angle + * @return + */ + public final void getAngle(final NyARDoublePoint3d o_angle) + { + double a,b,c; + double sina, cosa, sinb,cosb,sinc, cosc; + + if (this.m22 > 1.0) {// if( rot[2][2] > 1.0 ) { + this.m22 = 1.0;// rot[2][2] = 1.0; + } else if (this.m22 < -1.0) {// }else if( rot[2][2] < -1.0 ) { + this.m22 = -1.0;// rot[2][2] = -1.0; + } + cosb =this.m22;// cosb = rot[2][2]; + b = Math.acos(cosb); + sinb =Math.sin(b); + final double rot02=this.m02; + final double rot12=this.m12; + if (b >= 0.000001 || b <= -0.000001) { + cosa = rot02 / sinb;// cosa = rot[0][2] / sinb; + sina = rot12 / sinb;// sina = rot[1][2] / sinb; + if (cosa > 1.0) { + /* printf("cos(alph) = %f\n", cosa); */ + cosa = 1.0; + sina = 0.0; + } + if (cosa < -1.0) { + /* printf("cos(alph) = %f\n", cosa); */ + cosa = -1.0; + sina = 0.0; + } + if (sina > 1.0) { + /* printf("sin(alph) = %f\n", sina); */ + sina = 1.0; + cosa = 0.0; + } + if (sina < -1.0) { + /* printf("sin(alph) = %f\n", sina); */ + sina = -1.0; + cosa = 0.0; + } + a = Math.acos(cosa); + if (sina < 0) { + a = -a; + } + // + // sinc = (rot[2][1]*rot[0][2]-rot[2][0]*rot[1][2])/(rot[0][2]*rot[0][2]+rot[1][2]*rot[1][2]); + // cosc = -(rot[0][2]*rot[2][0]+rot[1][2]*rot[2][1])/(rot[0][2]*rot[0][2]+rot[1][2]*rot[1][2]); + final double tmp = (rot02 * rot02 + rot12 * rot12); + sinc = (this.m21 * rot02 - this.m20 * rot12) / tmp; + cosc = -(rot02 * this.m20 + rot12 * this.m21) / tmp; + // + + if (cosc > 1.0) { + /* printf("cos(r) = %f\n", cosc); */ + cosc = 1.0; + sinc = 0.0; + } + if (cosc < -1.0) { + /* printf("cos(r) = %f\n", cosc); */ + cosc = -1.0; + sinc = 0.0; + } + if (sinc > 1.0) { + /* printf("sin(r) = %f\n", sinc); */ + sinc = 1.0; + cosc = 0.0; + } + if (sinc < -1.0) { + /* printf("sin(r) = %f\n", sinc); */ + sinc = -1.0; + cosc = 0.0; + } + c = Math.acos(cosc); + if (sinc < 0) { + c = -c; + } + } else { + a = b = 0.0; + cosa = cosb = 1.0; + sina = sinb = 0.0; + cosc=this.m00;//cosc = rot[0];// cosc = rot[0][0]; + sinc=this.m01;//sinc = rot[1];// sinc = rot[1][0]; + if (cosc > 1.0) { + /* printf("cos(r) = %f\n", cosc); */ + cosc = 1.0; + sinc = 0.0; + } + if (cosc < -1.0) { + /* printf("cos(r) = %f\n", cosc); */ + cosc = -1.0; + sinc = 0.0; + } + if (sinc > 1.0) { + /* printf("sin(r) = %f\n", sinc); */ + sinc = 1.0; + cosc = 0.0; + } + if (sinc < -1.0) { + /* printf("sin(r) = %f\n", sinc); */ + sinc = -1.0; + cosc = 0.0; + } + c = Math.acos(cosc); + if (sinc < 0) { + c = -c; + } + } + o_angle.x = a;// wa.value=a;//*wa = a; + o_angle.y = b;// wb.value=b;//*wb = b; + o_angle.z = c;// wc.value=c;//*wc = c; + return; + } + /** + * 回転角から回転行列を計算してセットします。 + * @param i_x + * @param i_y + * @param i_z + */ + public final void setAngle(final double i_x, final double i_y, final double i_z) + { + final double sina = Math.sin(i_x); + final double cosa = Math.cos(i_x); + final double sinb = Math.sin(i_y); + final double cosb = Math.cos(i_y); + final double sinc = Math.sin(i_z); + final double cosc = Math.cos(i_z); + // Optimize + final double CACA = cosa * cosa; + final double SASA = sina * sina; + final double SACA = sina * cosa; + final double SASB = sina * sinb; + final double CASB = cosa * sinb; + final double SACACB = SACA * cosb; + + this.m00 = CACA * cosb * cosc + SASA * cosc + SACACB * sinc - SACA * sinc; + this.m01 = -CACA * cosb * sinc - SASA * sinc + SACACB * cosc - SACA * cosc; + this.m02 = CASB; + this.m10 = SACACB * cosc - SACA * cosc + SASA * cosb * sinc + CACA * sinc; + this.m11 = -SACACB * sinc + SACA * sinc + SASA * cosb * cosc + CACA * cosc; + this.m12 = SASB; + this.m20 = -CASB * cosc - SASB * sinc; + this.m21 = CASB * sinc - SASB * cosc; + this.m22 = cosb; + return; + } + /** + * i_in_pointを変換行列で座標変換する。 + * @param i_in_point + * @param i_out_point + */ + public final void getPoint3d(final NyARDoublePoint3d i_in_point,final NyARDoublePoint3d i_out_point) + { + final double x=i_in_point.x; + final double y=i_in_point.y; + final double z=i_in_point.z; + i_out_point.x=this.m00 * x + this.m01 * y + this.m02 * z; + i_out_point.y=this.m10 * x + this.m11 * y + this.m12 * z; + i_out_point.z=this.m20 * x + this.m21 * y + this.m22 * z; + return; + } + /** + * 複数の頂点を一括して変換する + * @param i_in_point + * @param i_out_point + * @param i_number_of_vertex + */ + public final void getPoint3dBatch(final NyARDoublePoint3d[] i_in_point,NyARDoublePoint3d[] i_out_point,int i_number_of_vertex) + { + for(int i=i_number_of_vertex-1;i>=0;i--){ + final NyARDoublePoint3d out_ptr=i_out_point[i]; + final NyARDoublePoint3d in_ptr=i_in_point[i]; + final double x=in_ptr.x; + final double y=in_ptr.y; + final double z=in_ptr.z; + out_ptr.x=this.m00 * x + this.m01 * y + this.m02 * z; + out_ptr.y=this.m10 * x + this.m11 * y + this.m12 * z; + out_ptr.z=this.m20 * x + this.m21 * y + this.m22 * z; + } + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/rotmatrix/NyARRotVector.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/rotmatrix/NyARRotVector.java new file mode 100644 index 0000000..f99fb71 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/transmat/rotmatrix/NyARRotVector.java @@ -0,0 +1,381 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat.rotmatrix; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARMat; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; +import jp.nyatla.nyartoolkit.core.types.NyARLinear; +import jp.nyatla.nyartoolkit.core.param.*; + +public class NyARRotVector +{ + + //publicメンバ達 + public double v1; + + public double v2; + + public double v3; + + //privateメンバ達 + + private NyARPerspectiveProjectionMatrix _projection_mat_ref; + + private double[][] _inv_cpara_array_ref; + + public NyARRotVector(NyARPerspectiveProjectionMatrix i_cmat) throws NyARException + { + NyARMat mat_a = new NyARMat(3, 3); + double[][] a_array = mat_a.getArray(); + + a_array[0][0] =i_cmat.m00; + a_array[0][1] =i_cmat.m01; + a_array[0][2] =i_cmat.m02; + a_array[1][0] =i_cmat.m10; + a_array[1][1] =i_cmat.m11; + a_array[1][2] =i_cmat.m12; + a_array[2][0] =i_cmat.m20; + a_array[2][1] =i_cmat.m21; + a_array[2][2] =i_cmat.m22; + + mat_a.matrixSelfInv(); + this._projection_mat_ref = i_cmat; + this._inv_cpara_array_ref = mat_a.getArray(); + //GCない言語のときは、ここで配列の所有権委譲してね! + } + + /** + * 2直線に直交するベクトルを計算する・・・だと思う。 + * @param i_linear1 + * @param i_linear2 + */ + public void exteriorProductFromLinear(NyARLinear i_linear1, NyARLinear i_linear2) + { + //1行目 + final NyARPerspectiveProjectionMatrix cmat= this._projection_mat_ref; + final double w1 = i_linear1.run * i_linear2.rise - i_linear2.run * i_linear1.rise; + final double w2 = i_linear1.rise * i_linear2.intercept - i_linear2.rise * i_linear1.intercept; + final double w3 = i_linear1.intercept * i_linear2.run - i_linear2.intercept * i_linear1.run; + + final double m0 = w1 * (cmat.m01 * cmat.m12 - cmat.m02 * cmat.m11) + w2 * cmat.m11 - w3 * cmat.m01;//w1 * (cpara[0 * 4 + 1] * cpara[1 * 4 + 2] - cpara[0 * 4 + 2] * cpara[1 * 4 + 1]) + w2 * cpara[1 * 4 + 1] - w3 * cpara[0 * 4 + 1]; + final double m1 = -w1 * cmat.m00 * cmat.m12 + w3 * cmat.m00;//-w1 * cpara[0 * 4 + 0] * cpara[1 * 4 + 2] + w3 * cpara[0 * 4 + 0]; + final double m2 = w1 * cmat.m00 * cmat.m11;//w1 * cpara[0 * 4 + 0] * cpara[1 * 4 + 1]; + final double w = Math.sqrt(m0 * m0 + m1 * m1 + m2 * m2); + this.v1 = m0 / w; + this.v2 = m1 / w; + this.v3 = m2 / w; + return; + } + + /** + * static int check_dir( double dir[3], double st[2], double ed[2],double cpara[3][4] ) Optimize:STEP[526->468] + * ベクトルの開始/終了座標を指定して、ベクトルの方向を調整する。 + * @param i_start_vertex + * @param i_end_vertex + * @param cpara + */ + public void checkVectorByVertex(final NyARDoublePoint2d i_start_vertex, final NyARDoublePoint2d i_end_vertex) throws NyARException + { + double h; + final double[][] inv_cpara = this._inv_cpara_array_ref; + //final double[] world = __checkVectorByVertex_world;// [2][3]; + final double world0 = inv_cpara[0][0] * i_start_vertex.x * 10.0 + inv_cpara[0][1] * i_start_vertex.y * 10.0 + inv_cpara[0][2] * 10.0;// mat_a->m[0]*st[0]*10.0+ + final double world1 = inv_cpara[1][0] * i_start_vertex.x * 10.0 + inv_cpara[1][1] * i_start_vertex.y * 10.0 + inv_cpara[1][2] * 10.0;// mat_a->m[3]*st[0]*10.0+ + final double world2 = inv_cpara[2][0] * i_start_vertex.x * 10.0 + inv_cpara[2][1] * i_start_vertex.y * 10.0 + inv_cpara[2][2] * 10.0;// mat_a->m[6]*st[0]*10.0+ + final double world3 = world0 + this.v1; + final double world4 = world1 + this.v2; + final double world5 = world2 + this.v3; + // + + //final double[] camera = __checkVectorByVertex_camera;// [2][2]; + final NyARPerspectiveProjectionMatrix cmat= this._projection_mat_ref; + //h = cpara[2 * 4 + 0] * world0 + cpara[2 * 4 + 1] * world1 + cpara[2 * 4 + 2] * world2; + h = cmat.m20 * world0 + cmat.m21 * world1 + cmat.m22 * world2; + if (h == 0.0) { + throw new NyARException(); + } + //final double camera0 = (cpara[0 * 4 + 0] * world0 + cpara[0 * 4 + 1] * world1 + cpara[0 * 4 + 2] * world2) / h; + //final double camera1 = (cpara[1 * 4 + 0] * world0 + cpara[1 * 4 + 1] * world1 + cpara[1 * 4 + 2] * world2) / h; + final double camera0 = (cmat.m00 * world0 + cmat.m01 * world1 + cmat.m02 * world2) / h; + final double camera1 = (cmat.m10 * world0 + cmat.m11 * world1 + cmat.m12 * world2) / h; + + //h = cpara[2 * 4 + 0] * world3 + cpara[2 * 4 + 1] * world4 + cpara[2 * 4 + 2] * world5; + h = cmat.m20 * world3 + cmat.m21 * world4 + cmat.m22 * world5; + if (h == 0.0) { + throw new NyARException(); + } + //final double camera2 = (cpara[0 * 4 + 0] * world3 + cpara[0 * 4 + 1] * world4 + cpara[0 * 4 + 2] * world5) / h; + //final double camera3 = (cpara[1 * 4 + 0] * world3 + cpara[1 * 4 + 1] * world4 + cpara[1 * 4 + 2] * world5) / h; + final double camera2 = (cmat.m00 * world3 + cmat.m01 * world4 + cmat.m02 * world5) / h; + final double camera3 = (cmat.m10 * world3 + cmat.m11 * world4 + cmat.m12 * world5) / h; + + final double v = (i_end_vertex.x - i_start_vertex.x) * (camera2 - camera0) + (i_end_vertex.y - i_start_vertex.y) * (camera3 - camera1); + if (v < 0) { + this.v1 = -this.v1; + this.v2 = -this.v2; + this.v3 = -this.v3; + } + } + /** + * int check_rotation( double rot[2][3] ) + * 2つのベクトル引数の調整をする? + * @param i_r + * @throws NyARException + */ + + public final static void checkRotation(NyARRotVector io_vec1, NyARRotVector io_vec2) throws NyARException + { + double w; + int f; + + double vec10 = io_vec1.v1; + double vec11 = io_vec1.v2; + double vec12 = io_vec1.v3; + double vec20 = io_vec2.v1; + double vec21 = io_vec2.v2; + double vec22 = io_vec2.v3; + + double vec30 = vec11 * vec22 - vec12 * vec21; + double vec31 = vec12 * vec20 - vec10 * vec22; + double vec32 = vec10 * vec21 - vec11 * vec20; + w = Math.sqrt(vec30 * vec30 + vec31 * vec31 + vec32 * vec32); + if (w == 0.0) { + throw new NyARException(); + } + vec30 /= w; + vec31 /= w; + vec32 /= w; + + double cb = vec10 * vec20 + vec11 * vec21 + vec12 * vec22; + if (cb < 0){ + cb=-cb;//cb *= -1.0; + } + final double ca = (Math.sqrt(cb + 1.0) + Math.sqrt(1.0 - cb)) * 0.5; + + if (vec31 * vec10 - vec11 * vec30 != 0.0) { + f = 0; + } else { + if (vec32 * vec10 - vec12 * vec30 != 0.0) { + w = vec11;vec11 = vec12;vec12 = w; + w = vec31;vec31 = vec32;vec32 = w; + f = 1; + } else { + w = vec10;vec10 = vec12;vec12 = w; + w = vec30;vec30 = vec32;vec32 = w; + f = 2; + } + } + if (vec31 * vec10 - vec11 * vec30 == 0.0) { + throw new NyARException(); + } + + double k1,k2,k3,k4; + double a, b, c, d; + double p1, q1, r1; + double p2, q2, r2; + double p3, q3, r3; + double p4, q4, r4; + + + k1 = (vec11 * vec32 - vec31 * vec12) / (vec31 * vec10 - vec11 * vec30); + k2 = (vec31 * ca) / (vec31 * vec10 - vec11 * vec30); + k3 = (vec10 * vec32 - vec30 * vec12) / (vec30 * vec11 - vec10 * vec31); + k4 = (vec30 * ca) / (vec30 * vec11 - vec10 * vec31); + + a = k1 * k1 + k3 * k3 + 1; + b = k1 * k2 + k3 * k4; + c = k2 * k2 + k4 * k4 - 1; + + d = b * b - a * c; + if (d < 0) { + throw new NyARException(); + } + r1 = (-b + Math.sqrt(d)) / a; + p1 = k1 * r1 + k2; + q1 = k3 * r1 + k4; + r2 = (-b - Math.sqrt(d)) / a; + p2 = k1 * r2 + k2; + q2 = k3 * r2 + k4; + if (f == 1) { + w = q1;q1 = r1;r1 = w; + w = q2;q2 = r2;r2 = w; + w = vec11;vec11 = vec12;vec12 = w; + w = vec31;vec31 = vec32;vec32 = w; + f = 0; + } + if (f == 2) { + w = p1;p1 = r1;r1 = w; + w = p2;p2 = r2;r2 = w; + w = vec10;vec10 = vec12;vec12 = w; + w = vec30;vec30 = vec32;vec32 = w; + f = 0; + } + + if (vec31 * vec20 - vec21 * vec30 != 0.0) { + f = 0; + } else { + if (vec32 * vec20 - vec22 * vec30 != 0.0) { + w = vec21;vec21 = vec22;vec22 = w; + w = vec31;vec31 = vec32;vec32 = w; + f = 1; + } else { + w = vec20;vec20 = vec22;vec22 = w; + w = vec30;vec30 = vec32;vec32 = w; + f = 2; + } + } + if (vec31 * vec20 - vec21 * vec30 == 0.0) { + throw new NyARException(); + } + k1 = (vec21 * vec32 - vec31 * vec22) / (vec31 * vec20 - vec21 * vec30); + k2 = (vec31 * ca) / (vec31 * vec20 - vec21 * vec30); + k3 = (vec20 * vec32 - vec30 * vec22) / (vec30 * vec21 - vec20 * vec31); + k4 = (vec30 * ca) / (vec30 * vec21 - vec20 * vec31); + + a = k1 * k1 + k3 * k3 + 1; + b = k1 * k2 + k3 * k4; + c = k2 * k2 + k4 * k4 - 1; + + d = b * b - a * c; + if (d < 0) { + throw new NyARException(); + } + r3 = (-b + Math.sqrt(d)) / a; + p3 = k1 * r3 + k2; + q3 = k3 * r3 + k4; + r4 = (-b - Math.sqrt(d)) / a; + p4 = k1 * r4 + k2; + q4 = k3 * r4 + k4; + if (f == 1) { + w = q3;q3 = r3;r3 = w; + w = q4;q4 = r4;r4 = w; + w = vec21;vec21 = vec22;vec22 = w; + w = vec31;vec31 = vec32;vec32 = w; + f = 0; + } + if (f == 2) { + w = p3;p3 = r3;r3 = w; + w = p4;p4 = r4;r4 = w; + w = vec20;vec20 = vec22;vec22 = w; + w = vec30;vec30 = vec32;vec32 = w; + f = 0; + } + + double e1 = p1 * p3 + q1 * q3 + r1 * r3; + if (e1 < 0) { + e1 = -e1; + } + double e2 = p1 * p4 + q1 * q4 + r1 * r4; + if (e2 < 0) { + e2 = -e2; + } + double e3 = p2 * p3 + q2 * q3 + r2 * r3; + if (e3 < 0) { + e3 = -e3; + } + double e4 = p2 * p4 + q2 * q4 + r2 * r4; + if (e4 < 0) { + e4 = -e4; + } + if (e1 < e2) { + if (e1 < e3) { + if (e1 < e4) { + io_vec1.v1 = p1; + io_vec1.v2 = q1; + io_vec1.v3 = r1; + io_vec2.v1 = p3; + io_vec2.v2 = q3; + io_vec2.v3 = r3; + } else { + io_vec1.v1 = p2; + io_vec1.v2 = q2; + io_vec1.v3 = r2; + io_vec2.v1 = p4; + io_vec2.v2 = q4; + io_vec2.v3 = r4; + } + } else { + if (e3 < e4) { + io_vec1.v1 = p2; + io_vec1.v2 = q2; + io_vec1.v3 = r2; + io_vec2.v1 = p3; + io_vec2.v2 = q3; + io_vec2.v3 = r3; + } else { + io_vec1.v1 = p2; + io_vec1.v2 = q2; + io_vec1.v3 = r2; + io_vec2.v1 = p4; + io_vec2.v2 = q4; + io_vec2.v3 = r4; + } + } + } else { + if (e2 < e3) { + if (e2 < e4) { + io_vec1.v1 = p1; + io_vec1.v2 = q1; + io_vec1.v3 = r1; + io_vec2.v1 = p4; + io_vec2.v2 = q4; + io_vec2.v3 = r4; + } else { + io_vec1.v1 = p2; + io_vec1.v2 = q2; + io_vec1.v3 = r2; + io_vec2.v1 = p4; + io_vec2.v2 = q4; + io_vec2.v3 = r4; + } + } else { + if (e3 < e4) { + io_vec1.v1 = p2; + io_vec1.v2 = q2; + io_vec1.v3 = r2; + io_vec2.v1 = p3; + io_vec2.v2 = q3; + io_vec2.v3 = r3; + } else { + io_vec1.v1 = p2; + io_vec1.v2 = q2; + io_vec1.v3 = r2; + io_vec2.v1 = p4; + io_vec2.v2 = q4; + io_vec2.v3 = r4; + } + } + } + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARDoublePoint2d.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARDoublePoint2d.java new file mode 100644 index 0000000..7cc4b44 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARDoublePoint2d.java @@ -0,0 +1,62 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types; + +public class NyARDoublePoint2d +{ + public double x; + public double y; + /** + * 配列ファクトリ + * @param i_number + * @return + */ + public static NyARDoublePoint2d[] createArray(int i_number) + { + NyARDoublePoint2d[] ret=new NyARDoublePoint2d[i_number]; + for(int i=0;i + * + */ +package jp.nyatla.nyartoolkit.core.types; + +public class NyARDoublePoint3d +{ + public double x; + public double y; + public double z; + /** + * 配列ファクトリ + * @param i_number + * @return + */ + public static NyARDoublePoint3d[] createArray(int i_number) + { + NyARDoublePoint3d[] ret=new NyARDoublePoint3d[i_number]; + for(int i=0;i + * + */ +package jp.nyatla.nyartoolkit.core.types; + +public class NyARIntPoint +{ + public int x; + + public int y; + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARIntRect.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARIntRect.java new file mode 100644 index 0000000..25fc49b --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARIntRect.java @@ -0,0 +1,43 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types; + +public class NyARIntRect +{ + public int x; + + public int y; + + public int w; + + public int h; +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARIntSize.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARIntSize.java new file mode 100644 index 0000000..0dcb9d2 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARIntSize.java @@ -0,0 +1,89 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types; + +import jp.nyatla.nyartoolkit.NyARException; + +public class NyARIntSize +{ + public int h; + + public int w; + public NyARIntSize() + { + this.w=0; + this.h=0; + return; + + } + + public NyARIntSize(int i_width,int i_height) + { + this.w=i_width; + this.h=i_height; + return; + } + + /** + * サイズが同一であるかを確認する。 + * + * @param i_width + * @param i_height + * @return + * @throws NyARException + */ + public boolean isEqualSize(int i_width, int i_height) + { + if (i_width == this.w && i_height == this.h) { + return true; + } + return false; + } + + /** + * サイズが同一であるかを確認する。 + * + * @param i_width + * @param i_height + * @return + * @throws NyARException + */ + public boolean isEqualSize(NyARIntSize i_size) + { + if (i_size.w == this.w && i_size.h == this.h) { + return true; + } + return false; + + } + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARLinear.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARLinear.java new file mode 100644 index 0000000..5e51041 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/NyARLinear.java @@ -0,0 +1,39 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types; + +public class NyARLinear +{ + public double rise;//y軸の増加量 + public double run;//x軸の増加量 + public double intercept;//切片 +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/INyARDoubleMatrix.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/INyARDoubleMatrix.java new file mode 100644 index 0000000..be61134 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/INyARDoubleMatrix.java @@ -0,0 +1,49 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types.matrix; + +public interface INyARDoubleMatrix +{ + /** + * 配列の内容を行列に設定する。 + * 遅いので余り使わないでね。 + * @param o_value + */ + public void setValue(double[] i_value); + /** + * 行列の内容を配列に返す。 + * 遅いので余り使わないでね。 + * @param o_value + */ + public void getValue(double[] o_value); + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix33.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix33.java new file mode 100644 index 0000000..c4147a2 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix33.java @@ -0,0 +1,77 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types.matrix; + +public class NyARDoubleMatrix33 implements INyARDoubleMatrix +{ + public double m00; + public double m01; + public double m02; + public double m10; + public double m11; + public double m12; + public double m20; + public double m21; + public double m22; + /** + * 遅いからあんまり使わないでね。 + */ + public void setValue(double[] i_value) + { + this.m00=i_value[0]; + this.m01=i_value[1]; + this.m02=i_value[2]; + this.m10=i_value[3]; + this.m11=i_value[4]; + this.m12=i_value[5]; + this.m20=i_value[6]; + this.m21=i_value[7]; + this.m22=i_value[8]; + return; + } + /** + * 遅いからあんまり使わないでね。 + */ + public void getValue(double[] o_value) + { + o_value[0]=this.m00; + o_value[1]=this.m01; + o_value[2]=this.m02; + o_value[3]=this.m10; + o_value[4]=this.m11; + o_value[5]=this.m12; + o_value[6]=this.m20; + o_value[7]=this.m21; + o_value[8]=this.m22; + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix34.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix34.java new file mode 100644 index 0000000..f87ec7c --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix34.java @@ -0,0 +1,80 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types.matrix; + +public class NyARDoubleMatrix34 implements INyARDoubleMatrix +{ + public double m00; + public double m01; + public double m02; + public double m03; + public double m10; + public double m11; + public double m12; + public double m13; + public double m20; + public double m21; + public double m22; + public double m23; + public void setValue(double[] i_value) + { + this.m00=i_value[0]; + this.m01=i_value[1]; + this.m02=i_value[2]; + this.m03=i_value[3]; + this.m10=i_value[4]; + this.m11=i_value[5]; + this.m12=i_value[6]; + this.m13=i_value[7]; + this.m20=i_value[8]; + this.m21=i_value[9]; + this.m22=i_value[10]; + this.m23=i_value[11]; + return; + } + public void getValue(double[] o_value) + { + o_value[0]=this.m00; + o_value[1]=this.m01; + o_value[2]=this.m02; + o_value[3]=this.m03; + o_value[4]=this.m10; + o_value[5]=this.m11; + o_value[6]=this.m12; + o_value[7]=this.m13; + o_value[8]=this.m20; + o_value[9]=this.m21; + o_value[10]=this.m22; + o_value[11]=this.m23; + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/stack/NyARIntPointStack.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/stack/NyARIntPointStack.java new file mode 100644 index 0000000..e0c5b93 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/stack/NyARIntPointStack.java @@ -0,0 +1,53 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types.stack; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.utils.NyObjectStack; + +public class NyARIntPointStack extends NyObjectStack +{ + public NyARIntPointStack(int i_length) + { + super(new NyARIntPoint[i_length]); + + } + + protected void onReservRequest(int i_start, int i_end, Object[] i_buffer) + { + for (int i = i_start; i < i_end; i++) { + i_buffer[i] = new NyARIntPoint(); + } + } + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/stack/NyARIntRectStack.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/stack/NyARIntRectStack.java new file mode 100644 index 0000000..ae8ce84 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core/types/stack/NyARIntRectStack.java @@ -0,0 +1,52 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.types.stack; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.types.NyARIntRect; +import jp.nyatla.utils.NyObjectStack; + +public class NyARIntRectStack extends NyObjectStack +{ + public NyARIntRectStack(int i_length) + { + super(new NyARIntRect[i_length]); + + } + + protected void onReservRequest(int i_start, int i_end, Object[] i_buffer) + { + for (int i = i_start; i < i_end; i++) { + i_buffer[i] = new NyARIntRect(); + } + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/NyARRasterDetector_QrCodeEdge.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/NyARRasterDetector_QrCodeEdge.java new file mode 100644 index 0000000..53cf6f7 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/NyARRasterDetector_QrCodeEdge.java @@ -0,0 +1,270 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasteranalyzer; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.types.stack.*; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.core.rasterreader.*; + +/** + * QRコードの4頂点候補を探そうとするクラス。 + * 未完成 + * + */ +public class NyARRasterDetector_QrCodeEdge +{ + private NyARIntRectStack _result; + + public NyARRasterDetector_QrCodeEdge(int i_result_max) + { + this._result = new NyARIntRectStack(i_result_max); + return; + } + + public NyARIntRectStack geResult() + { + return this._result; + } + + private boolean check_w1(int i_w1) + { + return i_w1>=1; + } + private boolean check_b1(int i_b1) + { + return i_b1 >= 2; + } + private boolean check_w2(int i_b1,int i_w2) + { + int v=i_w2*100/i_b1; + return (30<=v && v<=170); + } + private boolean check_b2(int i_b1,int i_b2) + { + int v=i_b2*100/i_b1; + //条件:(b1)/2の2~4倍 + return (200<=v && v<=400); + } + private boolean check_w3(int i_w2,int i_w3) + { + int v=i_w3*100/i_w2; + return (50<=v && v<=150); + } + private boolean check_b3(int i_b3,int i_b1) + { + int v=i_b3*100/i_b1; + return (50<=v && v<=150); + } + public void analyzeRaster(INyARRaster i_input) throws NyARException + { + INyARBufferReader buffer_reader=i_input.getBufferReader(); + assert (buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_BIN_8)); + + // 結果をクリア + this._result.clear(); + + NyARIntSize size = i_input.getSize(); + int x = 0; + int w1, b1, w2, b2, w3, b3; + w1 = b1 = w2 = b2 = w3 = b3 = 0; + + NyARIntRect item; + int[] line; + int s_pos, b2_spos,b3_spos; + b2_spos=0; + for (int y = size.h - 1-8; y >= 8; y--) { + line = ((int[][]) buffer_reader.getBuffer())[y]; + x = size.w - 1; + s_pos=0; + int token_id=0; + while(x>=0){ + switch(token_id){ + case 0: + // w1の特定 + w1 = 0; + for (; x >= 0; x--) { + if (line[x] == 0) { + // 検出条件確認:w1は2以上欲しいな。 + if (!check_w1(w1)) { + // 条件不十分 + continue; + }else{ + // 検出→次段処理へ + token_id=1; + } + break; + } + w1++; + } + break; + case 1: + // b1の特定 + b1 = 0; + s_pos = x; + for (; x >= 0; x--) { + if (line[x] > 0) { + // 検出条件確認:b1は1以上欲しいな。 + if (!check_b1(b1)){ + //条件不十分→白検出からやり直し + token_id=0; + }else{ + // 検出→次段処理へ + token_id=2; + } + break; + } + b1++; + } + break; + case 2: + // w2の特定 + w2 = 0; + for (; x >= 0; x--) { + if (line[x] == 0) { + // 検出条件確認:w2*10/b1は80-120以上欲しいな。 + if (!check_w2(b1,w2)) { + //条件不十分→w2→w1として、b1を解析 + w1=w2; + token_id=1; + }else{ + // 検出→次段処理へ +// w1=w2; +// token_id=11; + token_id=3; + } + break; + } + w2++; + } + break; + case 3: + // b2の特定 + b2 = 0; + b2_spos=x; + for (; x >= 0; x--) { + if (line[x] > 0){ + //条件:(w1+b1)/2の2~4倍 + + if (!check_b2(b1,b2)) { + // b2->b1と仮定して解析しなおす。 + if(check_w1(w2) && check_b1(b2)){ + w1 = w2; + b1 = b2; + s_pos=b2_spos; + token_id=2; + }else{ + + token_id=0; + } + }else{ + // 検出→次段処理へ +// token_id=10; + token_id=4; + } + break; + } + b2++; + } + break; + case 4: + // w3の特定 + w3 = 0; + for (; x >= 0; x--) { + if (line[x] == 0){ + if (!check_w3(w2,w3)) { + //w2→w1,b2->b1として解析しなおす。 + if(check_w1(w2) && check_b1(b2)){ + w1 = w2; + b1 = b2; + s_pos=b2_spos; + token_id=2; + }else{ + token_id=0; + } + }else{ + // 検出→次段処理へ +// w1=w3; +// token_id=10; + token_id=5; + } + break; + } + w3++; + } + break; + case 5: + // b3の特定 + b3 = 0; + b3_spos=x; + for (; x >= 0; x--) { + if (line[x] > 0) { + // 検出条件確認 + if (!check_b3(b3,b1)) { + if(check_w1(w2) && check_b1(b2)){ + //条件不十分→b3->b1,w3->w1として再解析 + w1=w3; + b1=b3; + s_pos=b3_spos; + token_id=2; + }else{ + token_id=0; + } + }else{ + // 検出→次段処理へ + token_id=10; + } + break; + } + b3++; + } + break; + case 10: + /* コード特定→保管 */ + item = (NyARIntRect)this._result.prePush(); + item.x = x; + item.y = y; + item.w =s_pos-x; + item.h=0; + /* 最大個数? */ + /* 次のコードを探す */ + token_id=0; + break; + default: + throw new NyARException(); + } + } + } + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/INyARRasterThresholdAnalyzer.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/INyARRasterThresholdAnalyzer.java new file mode 100644 index 0000000..13b2ff4 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/INyARRasterThresholdAnalyzer.java @@ -0,0 +1,45 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold; + + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; + +public interface INyARRasterThresholdAnalyzer +{ + public void analyzeRaster(INyARRaster i_input) throws NyARException; + + public int getThreshold(); + + public int getThreshold(int i_x, int i_y); +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/NyARRasterThresholdAnalyzer_PTile.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/NyARRasterThresholdAnalyzer_PTile.java new file mode 100644 index 0000000..5808614 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/NyARRasterThresholdAnalyzer_PTile.java @@ -0,0 +1,171 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * Pタイル法による閾値検出 + * + */ +public class NyARRasterThresholdAnalyzer_PTile implements INyARRasterThresholdAnalyzer +{ + private int _persentage; + + private int _threshold; + + /** + * @param i_persentage + * -100<=0<=100であること。 正の場合、黒点を基準にします。 負の場合、白点を基準にします。 + * (CMOSカメラの場合、基準点は白点の方が良い) + */ + public NyARRasterThresholdAnalyzer_PTile(int i_persentage) + { + assert (-100 <= i_persentage && i_persentage <= 100); + this._persentage = i_persentage; + } + + private int createHistgram(INyARBufferReader i_reader,NyARIntSize i_size, int[] o_histgram) throws NyARException + { + int[][] in_buf = (int[][]) i_reader.getBuffer(); + int[] histgram = o_histgram; + + // ヒストグラムを作成 + for (int i = 0; i < 256; i++) { + histgram[i] = 0; + } + int sum = 0; + for (int y = 0; y < i_size.h; y++) { + int sum2 = 0; + for (int x = 0; x < i_size.w; x++) { + int v = in_buf[y][x]; + histgram[v]++; + sum2 += v; + } + sum = sum + sum2 / i_size.w; + } + // 閾値ピクセル数確定 + int th_pixcels = i_size.w * i_size.h * this._persentage / 100; + + // 閾値判定 + int i; + if (th_pixcels > 0) { + // 黒点基準 + for (i = 0; i < 254; i++) { + th_pixcels -= histgram[i]; + if (th_pixcels <= 0) { + break; + } + } + } else { + // 白点基準 + for (i = 255; i > 1; i--) { + th_pixcels += histgram[i]; + if (th_pixcels >= 0) { + break; + } + } + } + // 閾値の保存 + return i; + } + + public void analyzeRaster(INyARRaster i_input) throws NyARException + { + final INyARBufferReader buffer_reader=i_input.getBufferReader(); + assert (buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8)); + int[] histgram = new int[256]; + this._threshold = createHistgram(buffer_reader,i_input.getSize(), histgram); + } + + /** + * デバック用の関数です。 ヒストグラムをラスタに書き出します。 + * + * @param i_output + * 書き出し先のラスタオブジェクト 256ピクセル以上の幅があること。 + */ + public void debugDrawHistgramMap(INyARRaster i_input, INyARRaster i_output) throws NyARException + { + INyARBufferReader in_buffer_reader=i_input.getBufferReader(); + INyARBufferReader out_buffer_reader=i_output.getBufferReader(); + assert (in_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8)); + assert (out_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8)); + NyARIntSize size = i_output.getSize(); + + int[][] out_buf = (int[][]) out_buffer_reader.getBuffer(); + // 0で塗りつぶし + for (int y = 0; y < size.h; y++) { + for (int x = 0; x < size.w; x++) { + out_buf[y][x] = 0; + } + } + // ヒストグラムを計算 + int[] histgram = new int[256]; + int threshold = createHistgram(in_buffer_reader,i_input.getSize(), histgram); + + // ヒストグラムの最大値を出す + int max_v = 0; + for (int i = 0; i < 255; i++) { + if (max_v < histgram[i]) { + max_v = histgram[i]; + } + } + // 目盛り + for (int i = 0; i < size.h; i++) { + out_buf[i][0] = 128; + out_buf[i][128] = 128; + out_buf[i][255] = 128; + } + // スケーリングしながら描画 + for (int i = 0; i < 255; i++) { + out_buf[histgram[i] * (size.h - 1) / max_v][i] = 255; + } + // 値 + for (int i = 0; i < size.h; i++) { + out_buf[i][threshold] = 255; + } + return; + } + + public int getThreshold() + { + return this._threshold; + } + + public int getThreshold(int i_x, int i_y) + { + return this._threshold; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/NyARRasterThresholdAnalyzer_SlidePTile.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/NyARRasterThresholdAnalyzer_SlidePTile.java new file mode 100644 index 0000000..09676da --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasteranalyzer/threshold/NyARRasterThresholdAnalyzer_SlidePTile.java @@ -0,0 +1,178 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.*; + + +/** + * 明点と暗点をPタイル法で検出して、その中央値を閾値とする。 + * + * + */ +public class NyARRasterThresholdAnalyzer_SlidePTile implements INyARRasterThresholdAnalyzer +{ + private int _persentage; + + private int _threshold; + + /** + * @param i_persentage + * 0<=50であること。白/黒マーカーの場合は10~20を推奨 正の場合、黒点を基準にします。 負の場合、白点を基準にします。 + * (CMOSカメラの場合、基準点は白点の方が良い) + */ + public NyARRasterThresholdAnalyzer_SlidePTile(int i_persentage) + { + assert (0 <= i_persentage && i_persentage <= 50); + this._persentage = i_persentage; + } + + private int createHistgram(INyARBufferReader i_reader,NyARIntSize i_size, int[] o_histgram) throws NyARException + { + int[][] in_buf = (int[][]) i_reader.getBuffer(); + int[] histgram = o_histgram; + + // ヒストグラムを作成 + for (int i = 0; i < 256; i++) { + histgram[i] = 0; + } + int sum = 0; + for (int y = 0; y < i_size.h; y++) { + int sum2 = 0; + for (int x = 0; x < i_size.w; x++) { + int v = in_buf[y][x]; + histgram[v]++; + sum2 += v; + } + sum = sum + sum2 / i_size.w; + } + // 閾値ピクセル数確定 + int th_pixcels = i_size.w * i_size.h * this._persentage / 100; + int th_wk; + int th_w, th_b; + + // 黒点基準 + th_wk = th_pixcels; + for (th_b = 0; th_b < 254; th_b++) { + th_wk -= histgram[th_b]; + if (th_wk <= 0) { + break; + } + } + // 白点基準 + th_wk = th_pixcels; + for (th_w = 255; th_w > 1; th_w--) { + th_wk -= histgram[th_w]; + if (th_wk <= 0) { + break; + } + } + // 閾値の保存 + return (th_w + th_b) / 2; + } + + public void analyzeRaster(INyARRaster i_input) throws NyARException + { + final INyARBufferReader buffer_reader=i_input.getBufferReader(); + assert (buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8)); + + int[] histgram = new int[256]; + // 閾値の基準値を出す。 + this._threshold = createHistgram(buffer_reader,i_input.getSize(), histgram); + return; + } + + /** + * ヒストグラムをラスタに書き出す。 + * + * @param i_output + */ + public void debugDrawHistgramMap(INyARRaster i_input, INyARRaster i_output) throws NyARException + { + INyARBufferReader in_buffer_reader=i_input.getBufferReader(); + INyARBufferReader out_buffer_reader=i_output.getBufferReader(); + assert (in_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8)); + assert (out_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8)); + + NyARIntSize size = i_output.getSize(); + + int[][] out_buf = (int[][]) out_buffer_reader.getBuffer(); + // 0で塗りつぶし + for (int y = 0; y < size.h; y++) { + for (int x = 0; x < size.w; x++) { + out_buf[y][x] = 0; + } + } + // ヒストグラムを計算 + int[] histgram = new int[256]; + int threshold = createHistgram(in_buffer_reader,i_input.getSize(), histgram); + for (int i = 255; i > 0; i--) { + histgram[i] = Math.abs(histgram[i]); + } + + // ヒストグラムの最大値を出す + int max_v = 0; + for (int i = 0; i < 255; i++) { + if (max_v < histgram[i]) { + max_v = histgram[i]; + } + } + // 目盛り + for (int i = 0; i < size.h; i++) { + out_buf[i][0] = 128; + out_buf[i][128] = 128; + out_buf[i][255] = 128; + } + // スケーリングしながら描画 + for (int i = 0; i < 255; i++) { + out_buf[histgram[i] * (size.h - 1) / max_v][i] = 255; + } + // 値 + for (int i = 0; i < size.h; i++) { + out_buf[i][threshold] = 255; + } + return; + } + + public int getThreshold() + { + return this._threshold; + } + + public int getThreshold(int i_x, int i_y) + { + return this._threshold; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_AreaAverage.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_AreaAverage.java new file mode 100644 index 0000000..1a83b0c --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_AreaAverage.java @@ -0,0 +1,88 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.INyARRasterFilter_GsToBin; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * 平均移動法を使った2値化フィルタ + * + */ +public class NyARRasterFilter_AreaAverage implements INyARRasterFilter_GsToBin +{ + private int _area = 8; + + public void doFilter(NyARGlayscaleRaster i_input, NyARBinRaster i_output) throws NyARException + { + final NyARIntSize size = i_output.getSize(); + final int[][] out_buf = (int[][]) i_output.getBufferReader().getBuffer(); + final int[][] in_buf = (int[][]) i_input.getBufferReader().getBuffer(); + assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); + assert (size.h % 8 == 0 && size.w % 8 == 0);//暫定実装なので。 + + final int area = this._area; + int y1 = area; + int x1 = area; + int y2 = size.h - area; + int x2 = size.w - area; + + for (int y = y1; y < y2; y++) { + int sum, nn; + sum = nn = 0; + for (int yy = y - area; yy < y + area + 1; yy++) { + for (int xx = x1 - area; xx < x1 + area; xx++) { + sum += in_buf[yy][xx]; + nn++; + } + } + boolean first = true; + for (int x = area; x < x2; x++) { + if (!first) { + for (int yy = y - area; yy < y + area; yy++) { + sum += in_buf[yy][x + area]; + sum -= in_buf[yy][x - area]; + } + } + first = false; + int th = (sum / nn); + + int g = in_buf[y][x]; + out_buf[y][x] = th < g ? 1 : 0; + } + } + return; + } + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_Threshold.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_Threshold.java new file mode 100644 index 0000000..0ba8de8 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_Threshold.java @@ -0,0 +1,69 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.INyARRasterFilter_GsToBin; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * 定数閾値による2値化をする。 + * + */ +public class NyARRasterFilter_Threshold implements INyARRasterFilter_GsToBin +{ + private int _threshold; + + public NyARRasterFilter_Threshold(int i_threshold) + { + this._threshold = i_threshold; + } + + public void doFilter(NyARGlayscaleRaster i_input, NyARBinRaster i_output) throws NyARException + { + assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); + + final int[][] out_buf = (int[][]) i_output.getBufferReader().getBuffer(); + final int[][] in_buf = (int[][]) i_input.getBufferReader().getBuffer(); + + int bp = 0; + NyARIntSize size = i_output.getSize(); + for (int y = 0; y < size.h - 1; y++) { + for (int x = 0; x < size.w; x++) { + out_buf[y][x] = in_buf[y][x] >= this._threshold ? 1 : 0; + bp += 3; + } + } + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbAve.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbAve.java new file mode 100644 index 0000000..c9da6f5 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbAve.java @@ -0,0 +1,89 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster; +import jp.nyatla.nyartoolkit.core.rasterfilter.INyARRasterFilter_RgbToGs; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; + +public class NyARRasterFilter_RgbAve implements INyARRasterFilter_RgbToGs +{ + public void doFilter(INyARRgbRaster i_input, NyARGlayscaleRaster i_output) throws NyARException + { + INyARBufferReader in_buffer_reader=i_input.getBufferReader(); + INyARBufferReader out_buffer_reader=i_output.getBufferReader(); + assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); + + int[][] out_buf = (int[][]) out_buffer_reader.getBuffer(); + byte[] in_buf = (byte[]) in_buffer_reader.getBuffer(); + + NyARIntSize size = i_output.getSize(); + switch (in_buffer_reader.getBufferType()) { + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24: + case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24: + convert24BitRgb(in_buf, out_buf, size); + break; + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32: + convert32BitRgbx(in_buf, out_buf, size); + break; + default: + throw new NyARException(); + } + return; + } + + private void convert24BitRgb(byte[] i_in, int[][] i_out, NyARIntSize i_size) + { + int bp = 0; + for (int y = 0; y < i_size.h; y++) { + for (int x = 0; x < i_size.w; x++) { + i_out[y][x] = ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)) / 3; + bp += 3; + } + } + return; + } + private void convert32BitRgbx(byte[] i_in, int[][] i_out, NyARIntSize i_size) + { + int bp = 0; + for (int y = 0; y < i_size.h; y++) { + for (int x = 0; x < i_size.w; x++) { + i_out[y][x] = ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff)) / 3; + bp += 4; + } + } + return; + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbMul.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbMul.java new file mode 100644 index 0000000..d0e21e2 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbMul.java @@ -0,0 +1,75 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster; +import jp.nyatla.nyartoolkit.core.rasterfilter.INyARRasterFilter_RgbToGs; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; + +public class NyARRasterFilter_RgbMul implements INyARRasterFilter_RgbToGs +{ + public void doFilter(INyARRgbRaster i_input, NyARGlayscaleRaster i_output) throws NyARException + { + INyARBufferReader in_buffer_reader=i_input.getBufferReader(); + INyARBufferReader out_buffer_reader=i_output.getBufferReader(); + assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); + + int[][] out_buf = (int[][]) in_buffer_reader.getBuffer(); + byte[] in_buf = (byte[]) out_buffer_reader.getBuffer(); + + NyARIntSize size = i_output.getSize(); + switch (in_buffer_reader.getBufferType()) { + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24: + case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24: + convert24BitRgb(in_buf, out_buf, size); + break; + default: + throw new NyARException(); + } + return; + } + + private void convert24BitRgb(byte[] i_in, int[][] i_out, NyARIntSize i_size) + { + int bp = 0; + for (int y = 0; y < i_size.h; y++) { + for (int x = 0; x < i_size.w; x++) { + i_out[y][x] = ((i_in[bp] & 0xff) * (i_in[bp + 1] & 0xff) * (i_in[bp + 2] & 0xff)) >> 16; + bp += 3; + } + } + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbOr.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbOr.java new file mode 100644 index 0000000..a2b6405 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbOr.java @@ -0,0 +1,75 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster; +import jp.nyatla.nyartoolkit.core.rasterfilter.INyARRasterFilter_RgbToGs; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; + +public class NyARRasterFilter_RgbOr implements INyARRasterFilter_RgbToGs +{ + public void doFilter(INyARRgbRaster i_input, NyARGlayscaleRaster i_output) throws NyARException + { + INyARBufferReader in_buffer_reader=i_input.getBufferReader(); + INyARBufferReader out_buffer_reader=i_output.getBufferReader(); + assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); + + final int[][] out_buf = (int[][]) out_buffer_reader.getBuffer(); + final byte[] in_buf = (byte[]) in_buffer_reader.getBuffer(); + + NyARIntSize size = i_output.getSize(); + switch (in_buffer_reader.getBufferType()) { + case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24: + case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24: + convert24BitRgb(in_buf, out_buf, size); + break; + default: + throw new NyARException(); + } + return; + } + + private void convert24BitRgb(byte[] i_in, int[][] i_out, NyARIntSize i_size) + { + int bp = 0; + for (int y = 0; y < i_size.h; y++) { + for (int x = 0; x < i_size.w; x++) { + i_out[y][x] = ((i_in[bp] & 0xff) | (i_in[bp + 1] & 0xff) | (i_in[bp + 2] & 0xff)); + bp += 3; + } + } + return; + } +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/temp/NyARLabeling_ARToolKit_X2.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/temp/NyARLabeling_ARToolKit_X2.java new file mode 100644 index 0000000..3c35e6b --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/temp/NyARLabeling_ARToolKit_X2.java @@ -0,0 +1,380 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.temp; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.core.labeling.*; + +/** + * NyARLabeling_ARToolKitと同じ処理をするけど、エリア計算にintを使う。 + * 画面サイズが1600x1600を超えると挙動が怪しくなる。 + * + */ +public class NyARLabeling_ARToolKit_X2 implements INyARLabeling +{ + private static final int WORK_SIZE = 1024 * 32;// #define WORK_SIZE 1024*32 + + private final NyARWorkHolder work_holder = new NyARWorkHolder(WORK_SIZE); + + private NyARIntSize _dest_size; + + private INyARLabelingImage _out_image; + + public void attachDestination(INyARLabelingImage i_destination_image) throws NyARException + { + // サイズチェック + NyARIntSize size = i_destination_image.getSize(); + this._out_image = i_destination_image; + + // NyLabelingImageのイメージ初期化(枠書き) + int[][] img = (int[][]) i_destination_image.getBufferReader().getBuffer(); + for (int i = 0; i < size.w; i++) { + img[0][i] = 0; + img[size.h - 1][i] = 0; + } + for (int i = 0; i < size.h; i++) { + img[i][0] = 0; + img[i][size.w - 1] = 0; + } + + // サイズ(参照値)を保存 + this._dest_size = size; + } + + public INyARLabelingImage getAttachedDestination() + { + return this._out_image; + } + + /** + * static ARInt16 *labeling2( ARUint8 *image, int thresh,int *label_num, int **area, double **pos, int **clip,int **label_ref, int LorR ) 関数の代替品 + * ラスタimageをラベリングして、結果を保存します。 Optimize:STEP[1514->1493] + * + * @param i_raster + * @throws NyARException + */ + public void labeling(NyARBinRaster i_raster) throws NyARException + { + int m, n; /* work */ + int i, j, k; + INyARLabelingImage out_image = this._out_image; + + // サイズチェック + NyARIntSize in_size = i_raster.getSize(); + this._dest_size.isEqualSize(in_size); + + final int lxsize = in_size.w;// lxsize = arUtil_c.arImXsize; + final int lysize = in_size.h;// lysize = arUtil_c.arImYsize; + int[][] label_img = (int[][]) out_image.getBufferReader().getBuffer(); + + // 枠作成はインスタンスを作った直後にやってしまう。 + + // ラベリング情報のリセット(ラベリングインデックスを使用) + out_image.reset(true); + + int[] label_idxtbl = out_image.getIndexArray(); + + int[] work2_pt; + int wk_max = 0; + + int label_pixel; + int[][] raster_buf = (int[][]) i_raster.getBufferReader().getBuffer(); + int[] line_ptr; + int[][] work2 = this.work_holder.work2; + int[] label_img_pt0, label_img_pt1; + for (j = 1; j < lysize - 1; j++) {// for (int j = 1; j < lysize - 1;j++, pnt += poff*2, pnt2 += 2) { + line_ptr = raster_buf[j]; + label_img_pt0 = label_img[j]; + label_img_pt1 = label_img[j - 1]; + for (i = 1; i < lxsize - 1; i++) {// for(int i = 1; i < lxsize-1;i++, pnt+=poff, pnt2++) { + // RGBの合計値が閾値より小さいかな? + if (line_ptr[i] == 0) { + // pnt1 = ShortPointer.wrap(pnt2, -lxsize);//pnt1 =&(pnt2[-lxsize]); + if (label_img_pt1[i] > 0) {// if( *pnt1 > 0 ) { + label_pixel = label_img_pt1[i];// *pnt2 = *pnt1; + + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + work2_pt[6] = j;// work2[((*pnt2)-1)*7+6] = j; + } else if (label_img_pt1[i + 1] > 0) {// }else if(*(pnt1+1) > 0 ) { + if (label_img_pt1[i - 1] > 0) {// if( *(pnt1-1) > 0 ) { + m = label_idxtbl[label_img_pt1[i + 1] - 1];// m =work[*(pnt1+1)-1]; + n = label_idxtbl[label_img_pt1[i - 1] - 1];// n =work[*(pnt1-1)-1]; + if (m > n) { + label_pixel = n;// *pnt2 = n; + // wk=IntPointer.wrap(work, 0);//wk = + // &(work[0]); + for (k = 0; k < wk_max; k++) { + if (label_idxtbl[k] == m) {// if( *wk == m ) + label_idxtbl[k] = n;// *wk = n; + } + } + } else if (m < n) { + label_pixel = m;// *pnt2 = m; + // wk=IntPointer.wrap(work,0);//wk = &(work[0]); + for (k = 0; k < wk_max; k++) { + if (label_idxtbl[k] == n) {// if( *wk == n ){ + label_idxtbl[k] = m;// *wk = m; + } + } + } else { + label_pixel = m;// *pnt2 = m; + } + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++; + work2_pt[1] += i; + work2_pt[2] += j; + work2_pt[6] = j; + } else if ((label_img_pt0[i - 1]) > 0) {// }else if(*(pnt2-1) > 0) { + m = label_idxtbl[(label_img_pt1[i + 1]) - 1];// m =work[*(pnt1+1)-1]; + n = label_idxtbl[label_img_pt0[i - 1] - 1];// n =work[*(pnt2-1)-1]; + if (m > n) { + + label_pixel = n;// *pnt2 = n; + for (k = 0; k < wk_max; k++) { + if (label_idxtbl[k] == m) {// if( *wk == m ){ + label_idxtbl[k] = n;// *wk = n; + } + } + } else if (m < n) { + label_pixel = m;// *pnt2 = m; + for (k = 0; k < wk_max; k++) { + if (label_idxtbl[k] == n) {// if( *wk == n ){ + label_idxtbl[k] = m;// *wk = m; + } + } + } else { + label_pixel = m;// *pnt2 = m; + } + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + } else { + + label_pixel = label_img_pt1[i + 1];// *pnt2 = + // *(pnt1+1); + + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + if (work2_pt[3] > i) {// if( + // work2[((*pnt2)-1)*7+3] > + // i ){ + work2_pt[3] = i;// work2[((*pnt2)-1)*7+3] = i; + } + work2_pt[6] = j;// work2[((*pnt2)-1)*7+6] = j; + } + } else if ((label_img_pt1[i - 1]) > 0) {// }else if( + // *(pnt1-1) > 0 ) { + label_pixel = label_img_pt1[i - 1];// *pnt2 = + // *(pnt1-1); + + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + if (work2_pt[4] < i) {// if( work2[((*pnt2)-1)*7+4] 0) {// }else if(*(pnt2-1) > 0) { + label_pixel = label_img_pt0[i - 1];// *pnt2 =*(pnt2-1); + + work2_pt = work2[label_pixel - 1]; + work2_pt[0]++;// work2[((*pnt2)-1)*7+0] ++; + work2_pt[1] += i;// work2[((*pnt2)-1)*7+1] += i; + work2_pt[2] += j;// work2[((*pnt2)-1)*7+2] += j; + if (work2_pt[4] < i) {// if( work2[((*pnt2)-1)*7+4] work2_pt[3]) { + temp_ptr[3] = work2_pt[3]; + } + if (temp_ptr[4] < work2_pt[4]) { + temp_ptr[4] = work2_pt[4]; + } + if (temp_ptr[5] > work2_pt[5]) { + temp_ptr[5] = work2_pt[5]; + } + if (temp_ptr[6] < work2_pt[6]) { + temp_ptr[6] = work2_pt[6]; + } + } + //ストア + for (int i = 0; i < i_number_of_label; i++) {// for(int i = 0; i < *label_num; i++ ) { + final NyARLabelingLabel label_pt = labels[i]; + final int temp_ptr[] = temp[i]; + label_pt.area=temp_ptr[0]; + label_pt.pos_x= (double)temp_ptr[1]/label_pt.area; + label_pt.pos_y= (double)temp_ptr[2]/label_pt.area; + label_pt.clip_l= temp_ptr[3]; + label_pt.clip_r= temp_ptr[4]; + label_pt.clip_t= temp_ptr[5]; + label_pt.clip_b= temp_ptr[6]; + } + return; + } +} + +/** + * NyARLabeling_O2のworkとwork2を可変長にするためのクラス + * + * + */ +final class NyARWorkHolder +{ + private final static int ARRAY_APPEND_STEP = 256; + + public final int[] work; + + public final int[][] work2; + + private int allocate_size; + + /** + * 最大i_holder_size個の動的割り当てバッファを準備する。 + * + * @param i_holder_size + */ + public NyARWorkHolder(int i_holder_size) + { + // ポインタだけははじめに確保しておく + this.work = new int[i_holder_size]; + this.work2 = new int[i_holder_size][]; + this.allocate_size = 0; + } + + /** + * i_indexで指定した番号までのバッファを準備する。 + * + * @param i_index + */ + public final void reserv(int i_index) throws NyARException + { + // アロケート済みなら即リターン + if (this.allocate_size > i_index) { + return; + } + // 要求されたインデクスは範囲外 + if (i_index >= this.work.length) { + throw new NyARException(); + } + // 追加アロケート範囲を計算 + int range = i_index + ARRAY_APPEND_STEP; + if (range >= this.work.length) { + range = this.work.length; + } + // アロケート + for (int i = this.allocate_size; i < range; i++) { + this.work2[i] = new int[8]; + } + this.allocate_size = range; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/temp/NyARRasterFilter_Edge.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/temp/NyARRasterFilter_Edge.java new file mode 100644 index 0000000..2806ca7 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/core2/temp/NyARRasterFilter_Edge.java @@ -0,0 +1,69 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core2.temp; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.INyARRasterFilter; +import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; + +/** + * エッジ検出フィルタ 入力 BUFFERFORMAT_INT2D 出力 BUFFERFORMAT_INT2D + */ +public class NyARRasterFilter_Edge implements INyARRasterFilter +{ + public void doFilter(INyARRaster i_input, INyARRaster i_output) throws NyARException + { + INyARBufferReader in_buffer_reader=i_input.getBufferReader(); + INyARBufferReader out_buffer_reader=i_output.getBufferReader(); + assert (in_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8)); + assert (out_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8)); + assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); + + int[][] out_buf = (int[][]) out_buffer_reader.getBuffer(); + int[][] in_buf = (int[][]) in_buffer_reader.getBuffer(); + + int bp = 0; + NyARIntSize size = i_output.getSize(); + for (int y = 1; y < size.h; y++) { + int prev = 128; + for (int x = 1; x < size.w; x++) { + int w = in_buf[y][x]; + out_buf[y][x] = (Math.abs(w - prev) + Math.abs(w - in_buf[y - 1][x])) / 2; + prev = w; + bp += 3; + } + } + return; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/detector/NyARDetectMarker.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/detector/NyARDetectMarker.java new file mode 100644 index 0000000..4932472 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/detector/NyARDetectMarker.java @@ -0,0 +1,295 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.detector; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.match.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.pickup.*; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; +import jp.nyatla.nyartoolkit.core.transmat.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*; +import jp.nyatla.nyartoolkit.core.types.*; + +class NyARDetectMarkerResult +{ + public int arcode_id; + + public int direction; + + public double confidence; + + public NyARSquare ref_square; +} + +class NyARDetectMarkerResultHolder +{ + public NyARDetectMarkerResult[] result_array = new NyARDetectMarkerResult[1]; + + /** + * result_holderを最大i_reserve_size個の要素を格納できるように予約します。 + * + * @param i_reserve_size + */ + public void reservHolder(int i_reserve_size) + { + if (i_reserve_size >= result_array.length) { + int new_size = i_reserve_size + 5; + result_array = new NyARDetectMarkerResult[new_size]; + for (int i = 0; i < new_size; i++) { + result_array[i] = new NyARDetectMarkerResult(); + } + } + } +} + +/** + * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。 + * + */ +public class NyARDetectMarker +{ + private static final int AR_SQUARE_MAX = 300; + + private boolean _is_continue = false; + + private NyARMatchPatt_Color_WITHOUT_PCA _match_patt; + + private INyARSquareDetector _square_detect; + + private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX); + + private NyARCode[] _codes; + + protected INyARTransMat _transmat; + + private double[] _marker_width; + + private int _number_of_code; + + // 検出結果の保存用 + private INyARColorPatt _patt; + + private NyARDetectMarkerResultHolder _result_holder = new NyARDetectMarkerResultHolder(); + + /** + * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。 + * + * @param i_param + * カメラパラメータを指定します。 + * @param i_code + * 検出するマーカーのARCode配列を指定します。配列要素のインデックス番号が、そのままgetARCodeIndex関数で 得られるARCodeインデックスになります。 例えば、要素[1]のARCodeに一致したマーカーである場合は、getARCodeIndexは1を返します。 + * 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。 + * @param i_marker_width + * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。 + * @param i_number_of_code + * i_codeに含まれる、ARCodeの数を指定します。 + * @throws NyARException + */ + public NyARDetectMarker(NyARParam i_param, NyARCode[] i_code, double[] i_marker_width, int i_number_of_code) throws NyARException + { + final NyARIntSize scr_size=i_param.getScreenSize(); + // 解析オブジェクトを作る + this._square_detect = new NyARSquareDetector(i_param.getDistortionFactor(),scr_size); + this._transmat = new NyARTransMat(i_param); + // 比較コードを保存 + this._codes = i_code; + // 比較コードの解像度は全部同じかな?(違うとパターンを複数種つくらないといけないから) + final int cw = i_code[0].getWidth(); + final int ch = i_code[0].getHeight(); + for (int i = 1; i < i_number_of_code; i++) { + if (cw != i_code[i].getWidth() || ch != i_code[i].getHeight()) { + // 違う解像度のが混ざっている。 + throw new NyARException(); + } + } + // 評価パターンのホルダを作る + this._patt = new NyARColorPatt_O3(cw, ch); + this._number_of_code = i_number_of_code; + + this._marker_width = i_marker_width; + // 評価器を作る。 + this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(); + //2値画像バッファを作る + this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h); + } + + private NyARBinRaster _bin_raster; + + private NyARRasterFilter_ARToolkitThreshold _tobin_filter = new NyARRasterFilter_ARToolkitThreshold(100); + + /** + * i_imageにマーカー検出処理を実行し、結果を記録します。 + * + * @param i_raster + * マーカーを検出するイメージを指定します。 + * @param i_thresh + * 検出閾値を指定します。0~255の範囲で指定してください。 通常は100~130くらいを指定します。 + * @return 見つかったマーカーの数を返します。 マーカーが見つからない場合は0を返します。 + * @throws NyARException + */ + public int detectMarkerLite(INyARRgbRaster i_raster, int i_threshold) throws NyARException + { + // サイズチェック + if (!this._bin_raster.getSize().isEqualSize(i_raster.getSize())) { + throw new NyARException(); + } + + // ラスタを2値イメージに変換する. + this._tobin_filter.setThreshold(i_threshold); + this._tobin_filter.doFilter(i_raster, this._bin_raster); + + NyARSquareStack l_square_list = this._square_list; + // スクエアコードを探す + this._square_detect.detectMarker(this._bin_raster, l_square_list); + + final int number_of_square = l_square_list.getLength(); + // コードは見つかった? + if (number_of_square < 1) { + // ないや。おしまい。 + return 0; + } + // 保持リストのサイズを調整 + this._result_holder.reservHolder(number_of_square); + + // 1スクエア毎に、一致するコードを決定していく + for (int i = 0; i < number_of_square; i++) { + NyARSquare square = (NyARSquare)l_square_list.getItem(i); + // 評価基準になるパターンをイメージから切り出す + if (!this._patt.pickFromRaster(i_raster, square)) { + // イメージの切り出しは失敗することもある。 + continue; + } + // パターンを評価器にセット + if (!this._match_patt.setPatt(this._patt)) { + // 計算に失敗した。 + throw new NyARException(); + } + // コードと順番に比較していく + int code_index = 0; + _match_patt.evaluate(_codes[0]); + double confidence = _match_patt.getConfidence(); + int direction = _match_patt.getDirection(); + for (int i2 = 1; i2 < this._number_of_code; i2++) { + // コードと比較する + _match_patt.evaluate(_codes[i2]); + double c2 = _match_patt.getConfidence(); + if (confidence > c2) { + continue; + } + // より一致するARCodeの情報を保存 + code_index = i2; + direction = _match_patt.getDirection(); + confidence = c2; + } + // i番目のパターン情報を保存する。 + final NyARDetectMarkerResult result = this._result_holder.result_array[i]; + result.arcode_id = code_index; + result.confidence = confidence; + result.direction = direction; + result.ref_square = square; + } + return number_of_square; + } + + /** + * i_indexのマーカーに対する変換行列を計算し、結果値をo_resultへ格納します。 直前に実行したdetectMarkerLiteが成功していないと使えません。 + * + * @param i_index + * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 + * @param o_result + * 結果値を受け取るオブジェクトを指定してください。 + * @throws NyARException + */ + public void getTransmationMatrix(int i_index, NyARTransMatResult o_result) throws NyARException + { + final NyARDetectMarkerResult result = this._result_holder.result_array[i_index]; + // 一番一致したマーカーの位置とかその辺を計算 + if (_is_continue) { + _transmat.transMatContinue(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result); + } else { + _transmat.transMat(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result); + } + return; + } + + /** + * i_indexのマーカーの一致度を返します。 + * + * @param i_index + * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 + * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。 + * @throws NyARException + */ + public double getConfidence(int i_index) + { + return this._result_holder.result_array[i_index].confidence; + } + + /** + * i_indexのマーカーの方位を返します。 + * + * @param i_index + * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 + * @return 0,1,2,3の何れかを返します。 + */ + public int getDirection(int i_index) + { + return this._result_holder.result_array[i_index].direction; + } + + /** + * i_indexのマーカーのARCodeインデックスを返します。 + * + * @param i_index + * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 + * @return + */ + public int getARCodeIndex(int i_index) + { + return this._result_holder.result_array[i_index].arcode_id; + } + + /** + * getTransmationMatrixの計算モードを設定します。 + * + * @param i_is_continue + * TRUEなら、transMatContinueを使用します。 FALSEなら、transMatを使用します。 + */ + public void setContinueMode(boolean i_is_continue) + { + this._is_continue = i_is_continue; + } + +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java new file mode 100644 index 0000000..91c09a9 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java @@ -0,0 +1,226 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.detector; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.match.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.pickup.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.transmat.*; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; +import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold; + +/** + * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。 + * + */ +public class NyARSingleDetectMarker +{ + private static final int AR_SQUARE_MAX = 100; + + private boolean _is_continue = false; + private NyARMatchPatt_Color_WITHOUT_PCA _match_patt; + private INyARSquareDetector _square_detect; + + private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX); + + private NyARCode _code; + + protected INyARTransMat _transmat; + + private double _marker_width; + + // 検出結果の保存用 + private int _detected_direction; + + private double _detected_confidence; + + private NyARSquare _detected_square; + + private INyARColorPatt _patt; + + /** + * 検出するARCodeとカメラパラメータから、1個のARCodeを検出するNyARSingleDetectMarkerインスタンスを作ります。 + * + * @param i_param + * カメラパラメータを指定します。 + * @param i_code + * 検出するARCodeを指定します。 + * @param i_marker_width + * ARコードの物理サイズを、ミリメートルで指定します。 + * @throws NyARException + */ + public NyARSingleDetectMarker(NyARParam i_param, NyARCode i_code, double i_marker_width) throws NyARException + { + final NyARIntSize scr_size=i_param.getScreenSize(); + // 解析オブジェクトを作る + this._square_detect = new NyARSquareDetector(i_param.getDistortionFactor(),scr_size); + this._transmat = new NyARTransMat(i_param); + // 比較コードを保存 + this._code = i_code; + this._marker_width = i_marker_width; + // 評価パターンのホルダを作る + this._patt = new NyARColorPatt_O3(_code.getWidth(), _code.getHeight()); + // 評価器を作る。 + this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(); + //2値画像バッファを作る + this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h); + } + + NyARBinRaster _bin_raster; + NyARRasterFilter_ARToolkitThreshold _tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100); + + /** + * i_imageにマーカー検出処理を実行し、結果を記録します。 + * + * @param i_raster + * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ + * と一致していなければなりません。 + * @return マーカーが検出できたかを真偽値で返します。 + * @throws NyARException + */ + public boolean detectMarkerLite(INyARRgbRaster i_raster,int i_threshold) throws NyARException + { + //サイズチェック + if(!this._bin_raster.getSize().isEqualSize(i_raster.getSize())){ + throw new NyARException(); + } + + //ラスタを2値イメージに変換する. + this._tobin_filter.setThreshold(i_threshold); + this._tobin_filter.doFilter(i_raster,this._bin_raster); + + + this._detected_square = null; + NyARSquareStack l_square_list = this._square_list; + // スクエアコードを探す + this._square_detect.detectMarker(this._bin_raster, l_square_list); + + + int number_of_square = l_square_list.getLength(); + // コードは見つかった? + if (number_of_square < 1) { + return false; + } + + // 評価基準になるパターンをイメージから切り出す + if (!this._patt.pickFromRaster(i_raster, (NyARSquare)l_square_list.getItem(0))) { + // パターンの切り出しに失敗 + return false; + } + // パターンを評価器にセット + if (!this._match_patt.setPatt(this._patt)) { + // 計算に失敗した。 + throw new NyARException(); + } + // コードと比較する + this._match_patt.evaluate(this._code); + int square_index = 0; + int direction = this._match_patt.getDirection(); + double confidence = this._match_patt.getConfidence(); + for (int i = 1; i < number_of_square; i++) { + // 次のパターンを取得 + this._patt.pickFromRaster(i_raster, (NyARSquare)l_square_list.getItem(i)); + // 評価器にセットする。 + this._match_patt.setPatt(this._patt); + // コードと比較する + this._match_patt.evaluate(this._code); + double c2 = this._match_patt.getConfidence(); + if (confidence > c2) { + continue; + } + // もっと一致するマーカーがあったぽい + square_index = i; + direction = this._match_patt.getDirection(); + confidence = c2; + } + // マーカー情報を保存 + this._detected_square = (NyARSquare)l_square_list.getItem(square_index); + this._detected_direction = direction; + this._detected_confidence = confidence; + return true; + } + + /** + * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。 + * 直前に実行したdetectMarkerLiteが成功していないと使えません。 + * + * @param o_result + * 変換行列を受け取るオブジェクトを指定します。 + * @throws NyARException + */ + public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException + { + // 一番一致したマーカーの位置とかその辺を計算 + if (this._is_continue) { + this._transmat.transMatContinue(this._detected_square,this._detected_direction,this._marker_width, o_result); + } else { + this._transmat.transMat(this._detected_square,this._detected_direction,this._marker_width, o_result); + } + return; + } + + /** + * 検出したマーカーの一致度を返します。 + * + * @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。 + * @throws NyARException + */ + public double getConfidence() + { + return this._detected_confidence; + } + + /** + * 検出したマーカーの方位を返します。 + * + * @return 0,1,2,3の何れかを返します。 + */ + public int getDirection() + { + return this._detected_direction; + } + + /** + * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。 + * + * @param i_is_continue + * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。 + */ + public void setContinueMode(boolean i_is_continue) + { + this._is_continue = i_is_continue; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/nyartoolkit/sample/RawFileTest.java b/tags/2.0.0/src/jp/nyatla/nyartoolkit/sample/RawFileTest.java new file mode 100644 index 0000000..55720ec --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/nyartoolkit/sample/RawFileTest.java @@ -0,0 +1,111 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.sample; + +import java.io.*; +import java.util.*; + +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; +import jp.nyatla.nyartoolkit.core.transmat.*; +import jp.nyatla.nyartoolkit.detector.NyARSingleDetectMarker; + + +/** + * 320x240のBGRA32で記録されたRAWイメージから、1種類のパターンを認識し、 + * その変換行列を1000回求め、それにかかったミリ秒時間を表示します。 + * + */ +public class RawFileTest +{ + private final String code_file = "../Data/patt.hiro"; + + private final String data_file = "../Data/320x240ABGR.raw"; + + private final String camera_file = "../Data/camera_para.dat"; + + public RawFileTest() + { + } + + + public void Test_arDetectMarkerLite() throws Exception + { + // AR用カメラパラメタファイルをロード + NyARParam ap = new NyARParam(); + ap.loadARParamFromFile(camera_file); + ap.changeScreenSize(320, 240); + + // AR用のパターンコードを読み出し + NyARCode code = new NyARCode(16, 16); + code.loadARPattFromFile(code_file); + + // 試験イメージの読み出し(320x240 BGRAのRAWデータ) + File f = new File(data_file); + FileInputStream fs = new FileInputStream(data_file); + byte[] buf = new byte[(int) f.length()]; + fs.read(buf); + INyARRgbRaster ra = NyARRgbRaster_BGRA.wrap(buf, 320, 240); + // Blank_Raster ra=new Blank_Raster(320, 240); + + // 1パターンのみを追跡するクラスを作成 + NyARSingleDetectMarker ar = new NyARSingleDetectMarker(ap, code, 80.0); + NyARTransMatResult result_mat = new NyARTransMatResult(); + ar.setContinueMode(false); + ar.detectMarkerLite(ra, 100); + ar.getTransmationMatrix(result_mat); + + // マーカーを検出 + Date d2 = new Date(); + for (int i = 0; i < 1000; i++) { + // 変換行列を取得 + ar.detectMarkerLite(ra, 100); + ar.getTransmationMatrix(result_mat); + } + Date d = new Date(); + System.out.println(d.getTime() - d2.getTime()); + } + + public static void main(String[] args) + { + + try { + RawFileTest t = new RawFileTest(); + // t.Test_arGetVersion(); + t.Test_arDetectMarkerLite(); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/tags/2.0.0/src/jp/nyatla/utils/DoubleValue.java b/tags/2.0.0/src/jp/nyatla/utils/DoubleValue.java new file mode 100644 index 0000000..5798a73 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/utils/DoubleValue.java @@ -0,0 +1,37 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.utils; + +public class DoubleValue +{ + public double value; +} \ No newline at end of file diff --git a/tags/2.0.0/src/jp/nyatla/utils/IntValue.java b/tags/2.0.0/src/jp/nyatla/utils/IntValue.java new file mode 100644 index 0000000..e4fa0da --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/utils/IntValue.java @@ -0,0 +1,37 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.utils; + +public class IntValue +{ + public int value; +} diff --git a/tags/2.0.0/src/jp/nyatla/utils/NyObjectStack.java b/tags/2.0.0/src/jp/nyatla/utils/NyObjectStack.java new file mode 100644 index 0000000..ccb4998 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/utils/NyObjectStack.java @@ -0,0 +1,169 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.utils; + +import jp.nyatla.nyartoolkit.NyARException; + +/** + * オンデマンド割り当てをするオブジェクト配列。 + * 配列には実体を格納します。 + */ +public abstract class NyObjectStack +{ + private final static int ARRAY_APPEND_STEP = 64; + + protected final Object[] _items; + + private int _allocated_size; + + protected int _length; + + /** + * 最大ARRAY_MAX個の動的割り当てバッファを準備する。 + * + * @param i_array + */ + public NyObjectStack(Object[] i_array) + { + // ポインタだけははじめに確保しておく + this._items = i_array; + // アロケート済サイズと、使用中個数をリセット + this._allocated_size = 0; + this._length = 0; + } + + /** + * ポインタを1進めて、その要素を予約し、その要素へのポインタを返します。 + * 特定型に依存させるときには、継承したクラスでこの関数をオーバーライドしてください。 + */ + public final Object prePush() throws NyARException + { + // 必要に応じてアロケート + if (this._length >= this._allocated_size) { + // 要求されたインデクスは範囲外 + if (this._length >= this._items.length) { + throw new NyARException(); + } + // 追加アロケート範囲を計算 + int range = this._length + ARRAY_APPEND_STEP; + if (range >= this._items.length) { + range = this._items.length; + } + // アロケート + this.onReservRequest(this._allocated_size, range, this._items); + this._allocated_size = range; + } + // 使用領域を+1して、予約した領域を返す。 + Object ret = this._items[this._length]; + this._length++; + return ret; + } + /** + * 見かけ上の要素数を1減らして、最後尾のアイテムを返します。 + * @return + */ + public final Object pop() throws NyARException + { + if(this._length<1){ + throw new NyARException(); + } + this._length--; + return this.getItem(this._length); + } + /** + * 0~i_number_of_item-1までの領域を予約します。 + * 予約すると、見かけ上の要素数は0にリセットされます。 + * @param i_number_of_reserv + */ + final public void reserv(int i_number_of_item) throws NyARException + { + // 必要に応じてアロケート + if (i_number_of_item >= this._allocated_size) { + // 要求されたインデクスは範囲外 + if (i_number_of_item >= this._items.length) { + throw new NyARException(); + } + // 追加アロケート範囲を計算 + int range = i_number_of_item+ARRAY_APPEND_STEP; + if (range >= this._items.length) { + range = this._items.length; + } + // アロケート + this.onReservRequest(this._allocated_size, range, this._items); + this._allocated_size = range; + } + //見かけ上の配列サイズを指定 + this._length=i_number_of_item; + return; + } + + + + /** + * この関数を継承先クラスで実装して下さい。 + * i_bufferの配列の、i_start番目からi_end-1番目までの要素に、オブジェクトを割り当てて下さい。 + * + * @param i_start + * @param i_end + * @param i_buffer + */ + protected abstract void onReservRequest(int i_start, int i_end, Object[] i_buffer); + /** + * 配列を返します。 + * + * @return + */ + public final Object[] getArray() + { + return this._items; + } + public final Object getItem(int i_index) + { + return this._items[i_index]; + } + /** + * 配列の見かけ上の要素数を返却します。 + * @return + */ + public final int getLength() + { + return this._length; + } + + /** + * 見かけ上の要素数をリセットします。 + */ + public final void clear() + { + this._length = 0; + } +} diff --git a/tags/2.0.0/src/jp/nyatla/utils/j2se/LabelingBufferdImage.java b/tags/2.0.0/src/jp/nyatla/utils/j2se/LabelingBufferdImage.java new file mode 100644 index 0000000..18d57d6 --- /dev/null +++ b/tags/2.0.0/src/jp/nyatla/utils/j2se/LabelingBufferdImage.java @@ -0,0 +1,202 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.utils.j2se; + +import java.awt.Graphics; +import java.awt.image.*; +import java.awt.color.*; +import java.awt.*; + + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.core.rasterreader.*; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.types.stack.*; + + +/** + * bitmapとして利用可能なラベリングイメージです。 + * + * + */ +public class LabelingBufferdImage extends BufferedImage +{ + public final static int COLOR_125_COLOR = 0;// 125色ラベルモード + + public final static int COLOR_256_MONO = 1;// 64階調モノクロモード + + public final static int COLOR_64_MONO = 2;// 64階調モノクロモード + + public final static int COLOR_32_MONO = 3;// 32階調モノクロモード + + public final static int COLOR_16_MONO = 4;// 16階調モノクロモード + + public final static int COLOR_8_MONO = 5;// 16階調モノクロモード + + private int[] _rgb_table; + + private int _number_of_color; + + /** + * i_width x i_heightの大きさのイメージを作成します。 + * + * @param i_width + * @param i_height + */ + public LabelingBufferdImage(int i_width, int i_height, int i_color_mode) + { + super(i_width, i_height, ColorSpace.TYPE_RGB); + // RGBテーブルを作成 + switch (i_color_mode) { + case COLOR_125_COLOR: + this._rgb_table = new int[125]; + this._number_of_color = 125; + for (int i = 0; i < 5; i++) { + for (int i2 = 0; i2 < 5; i2++) { + for (int i3 = 0; i3 < 5; i3++) { + this._rgb_table[((i * 5) + i2) * 5 + i3] = ((((i * 63) << 8) | (i2 * 63)) << 8) | (i3 * 63); + } + } + } + break; + case COLOR_256_MONO: + this._rgb_table = new int[256]; + this._number_of_color = 256; + for (int i = 0; i < 256; i++) { + this._rgb_table[i] = (i << 16) | (i << 8) | i; + } + break; + case COLOR_64_MONO: + this._rgb_table = new int[64]; + this._number_of_color = 64; + for (int i = 0; i < 64; i++) { + int m = (i * 4); + this._rgb_table[i] = (m << 16) | (m << 8) | m; + } + break; + case COLOR_32_MONO: + this._rgb_table = new int[32]; + this._number_of_color = 32; + for (int i = 0; i < 32; i++) { + int m = (i * 8); + this._rgb_table[i] = (m << 16) | (m << 8) | m; + } + break; + case COLOR_16_MONO: + this._rgb_table = new int[32]; + this._number_of_color = 16; + for (int i = 0; i < 16; i++) { + int m = (i * 8); + this._rgb_table[i] = (m << 16) | (m << 8) | m; + } + break; + } + } + + + + public void drawImage(NyARGlayscaleRaster i_raster) throws NyARException + { + assert (i_raster.getBufferReader().getBufferType() == INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8); + + int w = this.getWidth(); + int h = this.getHeight(); + // サイズをチェック + NyARIntSize size = i_raster.getSize(); + if (size.h > h || size.w > w) { + throw new NyARException(); + } + + int[][] limg; + // イメージの描画 + limg = (int[][]) i_raster.getBufferReader().getBuffer(); + for (int i = 0; i < h; i++) { + for (int i2 = 0; i2 < w; i2++) { + this.setRGB(i2, i, this._rgb_table[limg[i][i2] % this._number_of_color]); + } + } + return; + } + + public void drawImage(NyARBinRaster i_raster) throws NyARException + { + assert (i_raster.getBufferReader().getBufferType() == INyARBufferReader.BUFFERFORMAT_INT2D_BIN_8); + + int w = this.getWidth(); + int h = this.getHeight(); + // サイズをチェック + NyARIntSize size = i_raster.getSize(); + if (size.h > h || size.w > w) { + throw new NyARException(); + } + + int[][] limg; + // イメージの描画 + limg = (int[][]) i_raster.getBufferReader().getBuffer(); + for (int i = 0; i < h; i++) { + for (int i2 = 0; i2 < w; i2++) { + this.setRGB(i2, i, limg[i][i2] > 0 ? 255 : 0); + } + } + return; + } + + public void overlayData(NyARIntPointStack i_stack) + { + int count = i_stack.getLength(); + NyARIntPoint[] items = (NyARIntPoint[])i_stack.getArray(); + Graphics g = this.getGraphics(); + for (int i = 0; i < count; i++) { + int x = items[i].x; + int y = items[i].y; + g.setColor(Color.red); + g.drawLine(x - 5, y, x + 5, y); + g.drawLine(x, y + 5, x, y - 5); + } + return; + } + public void overlayData(NyARIntRectStack i_stack) + { + Color[] c=new Color[]{Color.cyan,Color.red,Color.green}; + int count = i_stack.getLength(); + NyARIntRect[] items = (NyARIntRect[])i_stack.getArray(); + Graphics g = this.getGraphics(); + for (int i = 0; i < count; i++) { + int x = items[i].x; + int y = items[i].y; + g.setColor(c[i%1]); + g.drawRect(x,y,items[i].w,items[i].h); + } + return; + } +} diff --git a/tags/2.0.0/test/jp/nyatla/nyartoolkit/dev/LabelingCamera.java b/tags/2.0.0/test/jp/nyatla/nyartoolkit/dev/LabelingCamera.java new file mode 100644 index 0000000..d22fdbf --- /dev/null +++ b/tags/2.0.0/test/jp/nyatla/nyartoolkit/dev/LabelingCamera.java @@ -0,0 +1,139 @@ +/* このソースは実験用のソースです。 + * 動いたり動かなかったりします。 + * + */ +package jp.nyatla.nyartoolkit.dev; + +import javax.media.*; + +import javax.media.util.BufferToImage; +import javax.media.format.*; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.jmf.utils.*; + +import java.awt.*; + + +import jp.nyatla.nyartoolkit.core.labeling.*; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.*; +import jp.nyatla.nyartoolkit.core2.rasteranalyzer.*; +import jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold.*; +import jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin.*; +import jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs.NyARRasterFilter_RgbAve; +import jp.nyatla.utils.j2se.LabelingBufferdImage; + +public class LabelingCamera extends Frame implements JmfCaptureListener { + private JmfNyARRaster_RGB _raster; + private JmfCameraCapture capture; + + + public LabelingCamera() throws NyARException, NyARException + { + setBounds(0, 0, 640 + 64, 720 + 64); + // キャプチャの準備 + capture = new JmfCameraCapture(320, 240, 30f, + JmfCameraCapture.PIXEL_FORMAT_RGB); + capture.setCaptureListener(this); + + // キャプチャイメージ用のラスタを準備 + this._raster = new JmfNyARRaster_RGB(320, 240); + } + + private NyARBinRaster _binraster1=new NyARBinRaster(320,240); + private NyARGlayscaleRaster _gsraster1=new NyARGlayscaleRaster(320,240); + private NyARLabelingImage _limage=new NyARLabelingImage(320, 240); + private LabelingBufferdImage _bimg=new LabelingBufferdImage(320, 240,LabelingBufferdImage.COLOR_256_MONO); + private LabelingBufferdImage _bimg2=new LabelingBufferdImage(320, 240,LabelingBufferdImage.COLOR_256_MONO); + + public void onUpdateBuffer(Buffer i_buffer) + { + try { + // キャプチャしたバッファをラスタにセット + _raster.setBuffer(i_buffer); + + Graphics g = getGraphics(); + //キャプチャ画像 + BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat()); + Image img = b2i.createImage(i_buffer); + this.getGraphics().drawImage(img, 32, 32, this); + + INyARRasterFilter_GsToBin filter_gs2bin; + //画像1 + INyARRasterFilter_RgbToGs filter_rgb2gs=new NyARRasterFilter_RgbAve(); + filter_rgb2gs.doFilter(_raster, _gsraster1); + this._bimg2.drawImage(this._gsraster1); + this.getGraphics().drawImage(this._bimg2, 32+320, 32,320+320+32,240+32,0,240,320,0, this); + + //画像2 + filter_gs2bin=new NyARRasterFilter_ARToolKitThreshold(128); + filter_gs2bin.doFilter(_gsraster1, _binraster1); + this._bimg.drawImage(_binraster1); + this.getGraphics().drawImage(this._bimg, 32, 32+240,320+32,240+32+240,0,240,320,0, this); + //画像3 + //threshold.debugDrawHistgramMap(_workraster, _workraster2); + //this._bimg2.setImage(this._workraster2); + //this.getGraphics().drawImage(this._bimg2, 32+320, 32+240,320+32+320,240+32+240,0,240,320,0, this); + + //画像4 + NyARRasterThresholdAnalyzer_SlidePTile threshold=new NyARRasterThresholdAnalyzer_SlidePTile(15); + threshold.analyzeRaster(_gsraster1); + filter_gs2bin=new NyARRasterFilter_AreaAverage(); + filter_gs2bin.doFilter(_gsraster1, _binraster1); + this._bimg.drawImage(_binraster1); + + NyARRasterDetector_QrCodeEdge detector=new NyARRasterDetector_QrCodeEdge(10000); + detector.analyzeRaster(_binraster1); + + this._bimg.overlayData(detector.geResult()); + + this.getGraphics().drawImage(this._bimg, 32, 32+480,320+32,480+32+240,0,240,320,0, this); + //画像5 + + +/* threshold2.debugDrawHistgramMap(_workraster, _workraster2); + this._bimg2.drawImage(this._workraster2); + this.getGraphics().drawImage(this._bimg2, 32+320, 32+480,320+32+320,480+32+240,0,240,320,0, this); +*/ + + // this.getGraphics().drawImage(this._bimg, 32, 32, this); + + + } catch (Exception e) { + e.printStackTrace(); + } + + } + private INyARLabeling labelingFactory(int i_idx) + { + switch(i_idx){ + case 0:{NyARLabeling_ARToolKit l=new NyARLabeling_ARToolKit();l.setThresh(4);return l;} + case 1:{return new NyLineLabeling();} + } + return null; + + + } + + private void startCapture() + { + try { + capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + try { + LabelingCamera mainwin = new LabelingCamera(); + mainwin.setVisible(true); + mainwin.startCapture(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/tags/2.0.0/test/jp/nyatla/nyartoolkit/dev/LabelingTest.java b/tags/2.0.0/test/jp/nyatla/nyartoolkit/dev/LabelingTest.java new file mode 100644 index 0000000..b9b7c28 --- /dev/null +++ b/tags/2.0.0/test/jp/nyatla/nyartoolkit/dev/LabelingTest.java @@ -0,0 +1,49 @@ +/* このソースは実験用のソースです。 + * 動いたり動かなかったりします。 + * + */ +package jp.nyatla.nyartoolkit.dev; + +import java.awt.Frame; +import java.io.File; +import java.io.FileInputStream; + +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.NyARRgbRaster_BGRA; +import jp.nyatla.nyartoolkit.core.labeling.*; + +import jp.nyatla.nyartoolkit.core2.rasteranalyzer.*; +import jp.nyatla.utils.j2se.*; + +public class LabelingTest extends Frame +{ + private final String data_file ="../Data/320x240ABGR.raw"; + public void drawImage() throws Exception + { + File f=new File(data_file); + FileInputStream fs=new FileInputStream(data_file); + byte[] buf=new byte[(int)f.length()]; + fs.read(buf); + NyARRgbRaster_BGRA ra=NyARRgbRaster_BGRA.wrap(buf, 320, 240); + NyARLabelingImage limage=new NyARLabelingImage(320,240); + INyARLabeling labeling=new NyARLabeling_ARToolKit(); +// INyARLabeling labeling=new NyLineLabeling(); + INyARRasterReaderFactory rf=new NyARRasterReaderFactory_RgbTotal(); + labeling.attachDestination(limage); + labeling.labeling(rf.createReader(ra)); + LabelingBufferdImage img=new LabelingBufferdImage(320,240,LabelingBufferdImage.COLOR_125_COLOR); + img.setLabelingImage(limage); + this.getGraphics().drawImage(img, 32,32,this); + } + public static void main(String[] args) + { + try{ + LabelingTest app=new LabelingTest(); + app.setVisible(true); + app.setBounds(0,0,640,480); + app.drawImage(); + }catch(Exception e){ + e.printStackTrace(); + } + } +}