private BitSet selectParticles(String str) {

		if ("selected".equalsIgnoreCase(str)) {
			return model.getParticleSelectionSet();
		}

		BitSet bs = genericSelect(str);
		if (bs != null)
			return bs;

		boolean found = false;
		int nop = model.getNumberOfParticles();
		bs = new BitSet(nop);

		Matcher matcher = WITHIN_RECTANGLE.matcher(str);
		if (matcher.find()) {
			found = true;
			int lp = str.indexOf("(");
			if (lp == -1)
				lp = str.indexOf("[");
			int rp = str.indexOf(")");
			if (rp == -1)
				rp = str.indexOf("]");
			float x = 0, y = 0, w = 0, h = 0;
			try {
				str = str.substring(lp + 1, rp).trim();
				String[] s = str.split(REGEX_SEPARATOR + "+");
				x = Float.valueOf(s[0].trim()).floatValue() * IR_CONVERTER;
				y = Float.valueOf(s[1].trim()).floatValue() * IR_CONVERTER;
				w = Float.valueOf(s[2].trim()).floatValue() * IR_CONVERTER;
				h = Float.valueOf(s[3].trim()).floatValue() * IR_CONVERTER;
			} catch (Exception e) {
				out(ScriptEvent.FAILED, "Script error at: " + str + "\n" + e);
				return null;
			}
			Particle p;
			for (int k = 0; k < nop; k++) {
				p = model.getParticle(k);
				if (p.getRx() >= x && p.getRx() <= x + w && p.getRy() >= y && p.getRy() <= y + h)
					bs.set(k);
			}
		}

		if (!found) {
			matcher = WITHIN_RADIUS.matcher(str);
			if (matcher.find()) {
				found = true;
				String s = str.substring(matcher.end()).trim();
				s = s.substring(0, s.indexOf(")"));
				if (RANGE.matcher(s).find()) {
					int lp = str.lastIndexOf("(");
					int rp = str.indexOf(")");
					s = str.substring(lp + 1, rp).trim();
					float r = Float.valueOf(s.substring(0, s.indexOf(",")).trim()).floatValue() * IR_CONVERTER;
					r *= r;
					s = s.substring(s.indexOf(",") + 1);
					int i0 = Math.round(Float.valueOf(s.substring(0, s.indexOf("-")).trim()).floatValue());
					int i1 = Math.round(Float.valueOf(s.substring(s.indexOf("-") + 1).trim()).floatValue());
					if (i0 < nop && i0 >= 0 && i1 < nop && i1 >= 0 && i1 >= i0) {
						Particle c;
						for (int k = i0; k <= i1; k++) {
							bs.set(k);
							c = model.getParticle(k);
							for (int m = 0; m < i0; m++) {
								if (bs.get(m))
									continue;
								if (model.getParticle(m).distanceSquare(c) < r)
									bs.set(m);
							}
							for (int m = i1 + 1; m < nop; m++) {
								if (bs.get(m))
									continue;
								if (model.getParticle(m).distanceSquare(c) < r)
									bs.set(m);
							}
						}
					}
				} else {
					int lp = str.indexOf("(");
					int rp = str.indexOf(")");
					s = str.substring(lp + 1, rp).trim();
					float r = Float.valueOf(s.substring(0, s.indexOf(",")).trim()).floatValue() * IR_CONVERTER;
					r *= r;
					int center = Math.round(Float.valueOf(s.substring(s.indexOf(",") + 1).trim()).floatValue());
					if (center < nop && center >= 0) {
						Particle c = model.getParticle(center);
						for (int k = 0; k < nop; k++) {
							if (k == center) {
								bs.set(k);
							} else {
								if (model.getParticle(k).distanceSquare(c) < r)
									bs.set(k);
							}
						}
					}
				}
			}
		}

		if (!found) {
			matcher = RANGE.matcher(str);
			if (matcher.find()) {
				found = true;
				String[] s = str.split("-");
				int start = Math.round(Float.valueOf(s[0].trim()).floatValue());
				int end = Math.round(Float.valueOf(s[1].trim()).floatValue());
				start = start < 0 ? 0 : start;
				end = end < nop ? end : nop - 1;
				for (int k = start; k <= end; k++)
					bs.set(k);
			}
		}

		if (!found) {
			matcher = INTEGER_GROUP.matcher(str);
			if (matcher.find()) {
				found = true;
				int lp = str.lastIndexOf("(");
				int rp = str.indexOf(")");
				if (lp != -1 && rp != -1) {
					str = str.substring(lp + 1, rp).trim();
				} else {
					if (lp != -1 || rp != -1) {
						out(ScriptEvent.FAILED, "Unbalanced parenthesis: " + str);
						return null;
					}
				}
				String[] s = str.split(REGEX_SEPARATOR + "+");
				for (String k : s) {
					int x = -1;
					try {
						x = Math.round(Float.valueOf(k.trim()).floatValue());
					} catch (NumberFormatException e) {
						out(ScriptEvent.FAILED, k + " cannot be parsed as an integer.");
						return null;
					}
					if (x >= 0 && x < nop)
						bs.set(x);
				}
			}
		}

		if (!found) {
			matcher = INDEX.matcher(str);
			if (matcher.find()) {
				found = true;
				int x = -1;
				try {
					x = Math.round(Float.valueOf(str.trim()).floatValue());
				} catch (NumberFormatException e) {
					out(ScriptEvent.FAILED, str + " cannot be parsed as an integer.");
					return null;
				}
				if (x >= 0 && x < nop)
					bs.set(x);
			}
		}

		if (found) {
			model.setParticleSelectionSet(bs);
		} else {
			out(ScriptEvent.FAILED, "Unrecognized expression: " + str);
		}

		return found ? bs : null;

	}